Carrierwave - 将图像大小调整为固定宽度
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需将其放入您的上传文件中:
此处的文档和示例: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:
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 withresize_to_limit
.我使用
10000
或任何非常大的数字让 Carrierwave 知道高度是自由的,只需调整宽度即可。@iWasRobbed:我认为这不是正确的解决方案。根据您粘贴的有关
resize_to_fit
的链接:调整大小后的图像的最大高度。如果省略,则默认为 new_width 的值。
因此,在您的情况下process :resize_to_fit => [100, nil]
相当于process :resize_to_fit => [100, 100]
这并不能保证您始终获得 100px 的固定宽度I use
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 caseprocess :resize_to_fit => [100, nil]
is equivalent toprocess :resize_to_fit => [100, 100]
which doesn't guarantee that you will always get the fixed width of 100px实际上不是一个更好的解决方案吗:
这样你根本不必限制高度
编辑:刚刚意识到这只适用于MiniMagick,对于RMagick你似乎别无选择,只能在高度上添加一个大数字
Wouldn't a better solution actually be:
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