载波-将图像调整为固定宽度 [英] Carrierwave - Resizing images to fixed width

查看:38
本文介绍了载波-将图像调整为固定宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RMagick,并希望将图像调整为100px的固定宽度,并按比例缩放高度。例如,如果用户要上传300x900px,我希望将其缩放到100x300px。

I'm using RMagick and want my images to be resized to a fixed width of 100px, and scale the height proportionally. For example, if a user were to upload a 300x900px, I would like it to be scaled to 100x300px.

推荐答案

在您的上传文件中:

class ImageUploader < CarrierWave::Uploader::Base

  version :resized do
    # returns an image with a maximum width of 100px 
    # while maintaining the aspect ratio
    # 10000 is used to tell CW that the height is free 
    # and so that it will hit the 100 px width first
    process :resize_to_fit => [100, 10000]
  end

end

文档和此处的示例: http://www.imagemagick.org/RMagick/doc/image3。 html#resize_to_fit

请记住,如果 resize_to_fit 尺寸小于100像素,则会放大图像。如果您不希望这样做,请用 resize_to_limit 代替。

Keep in mind, resize_to_fit will scale up images if they are smaller than 100px. If you don't want it to do that, then replace that with resize_to_limit.

这篇关于载波-将图像调整为固定宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆