Forum: Ruby on Rails RMagick Resizing Issue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstien (Guest)
on 2005-12-19 17:35
(Received via mailing list)
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
A78fabe22aa7df17f05cb02e622e20a7?d=identicon&s=25 Jerrett Taylor (Guest)
on 2005-12-19 19:51
(Received via mailing list)
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: http://www.simplesystems.org/RMagick/doc/
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstien (Guest)
on 2005-12-19 23:19
(Received via mailing list)
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
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstien (Guest)
on 2005-12-20 00:07
(Received via mailing list)
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
This topic is locked and can not be replied to.