On Apr 11, 2005 4:04 PM, Timothy Hunter <cyclists / nc.rr.com> wrote:
> Joe Van Dyk wrote:
> > I have images that have a bunch of different aspect ratios.  When I
> > generate a thumbnail of those different images, I want all the
> > thumbnails to have the same image ratio.  Currently, I can only have
> > the thumbnails have the same width OR the same height, and not both at
> > the same time.
> >
> > Any ideas as for the algorithm to do this?  Or is there some function
> > inside RMagick that does something similar to this?
> >
> Do you mean you want to resize an image to a specific size without
> retaining its current aspect ratio? Just specify the desired new width
> and height when you call #resize:
> http://www.simplesystems.org/RMagick/doc/image3.html#resize
> 
> In general you can use the #change_geometry method to compute a new
> image geometry. Here's a couple of links:
> 
> http://www.simplesystems.org/RMagick/doc/comtasks.html#resizing
> http://www.simplesystems.org/RMagick/doc/image1.html#change_geometry

No, I want to resize an image to a specific size and maintain its
current aspect ratio.  This will probably involve cropping, I'd guess.

Here's my first attempt (psuedo untested code)

thumbnail_width = 100.0
thumbnail_height = 75.0
thumbnail_aspect_ratio = thumbnail_width / thumbnail_height

image = RMagick::Image.new( whatever in here )
image_aspect_ratio = image.rows.to_f / image.cols.to_f

if thumbnail_aspect_ratio > image_aspect_ratio
  # Image is too wide, geometry string should restrict height
  geometry_string = "x#{thumbnail_height}"
else
  # Image is either just right or too tall, geometry string should 
  # restrict width
  geometry_string = "#{thumbnail_width}"
end

thumbnail_image = image.change_geometry(geometry_string) { ... }

# Crop image to the thumbnail's width and height
thumbnail_image.crop(RMagick::CENTER_GRAVITY, 
                                          thumbnail_width, thumbnail_height)



That should work, right?  (don't have access to RMagick here yet..)