tags:

views:

20

answers:

1

Is there a way for me to scale and image in rmagick where i set the width, and the height auto scales so the image contain the same proportions?

A: 

I use the resize_to_fit method, which will use the parameters supplied as the maximum width/height, but will keep the aspect ration. So, something like this:

@scaled = @image.resize_to_fit 640 640

That will ensure that either the width or height is no greater than 640, but will not stretch the image making it look funny. So, you might end up with 640x480 or 480x640. There is also a resize_to_fit! method that converts in place

If you want to resize to a given width without respect to a bounding box, you will have to write a helper function. Something like this:

@img = Magick::Image::read(file_name).first

def resize_by_width image new_width  
  @new_height = new_width * image.x_resolution.to_f / image.y_resolution.to_f
  new_image = image.scale(new_width, new_height)

  return new_image
end

@resized = resize_by_width @img 1024

Hope that helps!

Travis
you're awesome, thanks!
hojberg