RMagick Resizing Issue

Hello ~

I have RMagick and file_column working in RoR. In my model I am setting
up
some standard sizes for the submitted photos. The photo files are
created
and named correctly, but the sizes are wrong. Only the second argument
of
my geometry call is used, with the width dimension being resized
proportionately.

file_column :image, :magick => { :versions => { “medium” => “180x240”,
“large” => “270x360” } }

So all my images are 240 pixels high or 360 pixels high, but not the
correct
width. Is there a toggle for resizing proportionately, or is my call
wrong?
I have been using this page for reference:
http://www.kanthak.net/opensource/file_column/

Thx in advance for your help!

~ Ben

Sounds like file_column is using change_geometry, which will resize
while maintaining aspect ratio. The geometry that you pass to it will
resize the image with those as constraints, meaning the resulting image
will be no larger (either way) than your specifications.

If you want to change the aspect ratio you should use resize or crop the
image. I’d suggest resizing based on the smallest edge, and then
cropping the image to the exact size that you want - otherwise you are
going to end up with distorted images.

Rmagick has good docs: RMagick 1.15.0 User's Guide and Reference

Yes, that documentation helped. I hacked this quick method into the
vendor/plugins/file_column/lib/magick_file_column.rb

#WILL NOT MAINTAIN ASPECT RATIO
def resize_image_raw(img, geometry, path)
new_img = img.change_geometry(geometry) do |c, r, i|
tempstuff = geometry.split(“x”)
temp1 = Integer(tempsomething[0])
temp2 = Integer(tempsomething[1])
i.resize(temp1, temp2)
end
new_img.write path
end

~ Ben

Darn, posted sample with typos! If you want to resize an image to a
specific size, you can do this:

#WILL NOT MAINTAIN ASPECT RATIO
def resize_image_raw(img, geometry, path)
new_img = img.change_geometry(geometry) do |c, r, i|
tempstuff = geometry.split(“x”)
temp1 = Integer(tempstuff[0])
temp2 = Integer(tempstuff[1])
i.resize(temp1, temp2)
end
new_img.write path
end

But as Jerrett pointed out a more elegant solution is to resize, then
crop.
I agree, here is how to do it:

def resize_image_resize_crop(img, geometry, path, name)
  new_img = img.change_geometry(geometry) do |c, r, i|
      i.resize(c,r).crop(0,0,YOUR_WIDTH_HERE,YOUR_HEIGHT_HERE)
  end
  new_img.write path
end

Now to make sure all images are the same size, pass the geometry object

“90x”. This will make all images have a width of 90 with the correct
aspect
ratio. You could also pass “x90” if you want the height to be static.
The
crop will then make the images the same size.

~ Ben