Ruby-on-rails 使用 Paperclip 调整图像大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2389723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:22:09  来源:igfitidea点击:

Resize an image with Paperclip

ruby-on-railsfile-uploadimage-manipulationimagemagickpaperclip

提问by Yuval Karmi

The Paperclip plugin for Rails has a resize option that keeps the image in proportion. According to Ryan Bates' Paperclip Railscast, to make sure that option is on, you have to add a greater-than sign in the end of the size for the style you're looking to resize, as such:

Rails 的 Paperclip 插件有一个调整大小的选项,可以保持图像的比例。根据 Ryan Bates 的Paperclip Railscast,要确保该选项处于启用状态,您必须在要调整大小的样式的大小末尾添加一个大于号,如下所示:

:styles => { :small => "160x160>" }

I'm looking for Paperclip to resize my image to an exact size, even if that means it being out of proportion, so I figured that removing the greater-than sign would do the trick, as such:

我正在寻找 Paperclip 将我的图像调整为精确大小,即使这意味着它不成比例,所以我认为删除大于号可以解决问题,因此:

:styles => { :small => "160x160" }

Well, turns out nothing happens. So the greater-than sign is redundant in the first place. I'm still left with the following question, though:

好吧,事实证明什么也没发生。所以大于号首先是多余的。不过,我仍然有以下问题:

How would I force the image into a set size, ignoring proportions?

我如何将图像强制为设定大小,而忽略比例?

Thanks!

谢谢!

回答by Austin Fitzpatrick

You can use "160x160#" which will scale and crop to exactly that size, which is unique to paperclip. Otherwise you can use any of the ImageMagick geometry strings, detailed here:

您可以使用“160x160#”,它将缩放和裁剪到恰好那个大小,这是回形针独有的。否则,您可以使用任何 ImageMagick 几何字符串,详情如下:

ImageMagick Geometry

ImageMagick 几何

But I'll quote the one you're interested in:

但我会引用你感兴趣的一个:

"160x160!"

Width and height emphatically given, original aspect ratio ignored.

着重给出宽度和高度,忽略原始纵横比。