rmagick自动缩放比例

时间:2010-10-30 10:01:00

标签: ruby rmagick

有没有办法让我在rmagick中进行缩放和成像,我设置宽度,高度自动缩放以使图像包含相同的比例?

1 个答案:

答案 0 :(得分:4)

我使用resize_to_fit方法,它将使用提供的参数作为最大宽度/高度,但将保持宽高比。所以,像这样:

@scaled = @image.resize_to_fit 640 640

这将确保宽度或高度不大于640,但不会拉伸图像使其看起来很有趣。因此,您最终可能会使用640x480或480x640。还有一个resize_to_fit!转换的方法

如果要在不考虑边界框的情况下调整到给定宽度,则必须编写辅助函数。像这样:

@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

希望有所帮助!

相关问题