开发者

rmagick auto scaling with proportions

Is there a way for me to scale and image in rmagick where i set the width, and the height a开发者_如何学运维uto scales so the image contain the same proportions?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜