Reputation: 3410
I have a problem with resizing image with Carrierwave-MiniMagick-ImageMagick.
I wrote custom method of resizing, cause I need to merge 2 images together and make some processing on them, so standard process
methods from MiniMagick are not enough. The problem is with resize method. I need to take center part of the image, but it returns me the top part.
def merge
manipulate! do |img|
img.resize '180x140^' # problem is here
...
img
end
end
Thanks for any help!
Upvotes: 0
Views: 1019
Reputation: 4930
This is what resize_to_fill
does:
Resize the image to fit within the specified dimensions while retaining the aspect ratio of the original image. If necessary, crop the image in the larger dimension.
Example:
image = ImageList.new(Rails.root.join('app/assets/images/image.jpg'))
thumb = image.resize_to_fill(1200, 630)
thumb.write('thumb.jpg')
The method takes a third argument which is gravity, but it's CenterGravity
by default.
Upvotes: 1
Reputation: 10898
I would approach this as follows:
Something like this should do it:
def merge
manipulate! do |img|
img.resize '180x180' # resize to 180px square
img.shave '0x20' # Removes 20px from top and bottom edges
img # Returned image should be 180x140, cropped from the centre
end
end
Of course, this assumes your input image is always a square. If it wasn't square and you've got your heart set on the 180x140 ratio, you could do something like this:
def merge
manipulate! do |img|
if img[:width] <= img[:height]
# Image is tall ...
img.resize '180' # resize to 180px wide
pixels_to_remove = ((img[:height] - 140)/2).round # calculate amount to remove
img.shave "0x#{pixels_to_remove}" # shave off the top and bottom
else
# Image is wide
img.resize 'x140' # resize to 140px high
pixels_to_remove = ((img[:width] - 180)/2).round # calculate amount to remove
img.shave "#{pixels_to_remove}x0" # shave off the sides
end
img # Returned image should be 180x140, cropped from the centre
end
end
Upvotes: 2
Reputation: 23278
You should use crop
instead of resize
.
Take a look at crop ImageMagick description of crop command here: http://www.imagemagick.org/script/command-line-options.php#crop
MiniMagick just a wrapper around ImageMagick, so all arguments are the same.
Upvotes: 0