Carrierwave - 将图像大小调整为固定宽度

发布于 2024-12-22 04:28:48 字数 95 浏览 4 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

是你 2024-12-29 04:28:48

只需将其放入您的上传文件中:

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" imagemagick.org/RMagick/doc/image3.html#resize_to_fit

请记住,如果图像小于 100 像素,resize_to_fit 将放大图像。如果您不希望它这样做,请将其替换为 resize_to_limit

Just put this in your uploader file:

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

Documentation and example here: http://www.imagemagick.org/RMagick/doc/image3.html#resize_to_fit

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.

酷遇一生 2024-12-29 04:28:48

我使用

process :resize_to_fit => [100, 10000]

10000 或任何非常大的数字让 Carrierwave 知道高度是自由的,只需调整宽度即可。

@iWasRobbed:我认为这不是正确的解决方案。根据您粘贴的有关resize_to_fit的链接:调整大小后的图像的最大高度。如果省略,则默认为 new_width 的值。 因此,在您的情况下 process :resize_to_fit => [100, nil] 相当于 process :resize_to_fit => [100, 100] 这并不能保证您始终获得 100px 的固定宽度

I use

process :resize_to_fit => [100, 10000]

Use 10000 or any very big number to let Carrierwave know the height is free, just resize to the width.

@iWasRobbed: I don't think that's the correct solution. According to the link you pasted about resize_to_fit: The maximum height of the resized image. If omitted it defaults to the value of new_width. So in your case process :resize_to_fit => [100, nil] is equivalent to process :resize_to_fit => [100, 100] which doesn't guarantee that you will always get the fixed width of 100px

心碎的声音 2024-12-29 04:28:48

实际上不是一个更好的解决方案吗:

process :resize_to_fit => [100, -1]

这样你根本不必限制高度

编辑:刚刚意识到这只适用于MiniMagick,对于RMagick你似乎别无选择,只能在高度上添加一个大数字

Wouldn't a better solution actually be:

process :resize_to_fit => [100, -1]

This way you don't have to limit height at all

EDIT: Just realized this only works with MiniMagick, For RMagick you seem to have no option but to add a large number to the height

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文