Rails Paperclip插件 - 调整大小的样式选项

25

我想将图片调整到固定宽度,但高度要等比例缩放。

我尝试了许多操作符:

380x242# 380x242> 380!x242 380x242<

但都没有达到预期的效果。请问有什么方法吗?我希望图片可以填满或调整到380像素宽,然后按照同样的比例缩小高度。

5个回答

56

2
如果可以的话,我会给你两个赞。这让我的一天完全得救了,而不是继续折腾 :covert_options。干杯 - Damon Aw
1
根据你提供的链接,应该只是380吧? - Puce
似乎现在可以只使用宽度而不需要加上x符号。我猜想自从我的回答写下来以后,过去的四年里他们已经改变了这个规则。同时,我认为380x(带有x)仍然可以用于向后兼容。 - Slobodan Kovacevic

41
"#"是Paperclip使用的参数,用于确定是否需要对图片进行裁剪。使用"100x100#"将精确地按照该尺寸缩放和裁剪图片。"%@!<>"是ImageMagick中使用的Geometry String的参数。以下是可以用于调整图像大小的ImageMagick geometry字符串:
  • 忽略纵横比('!')
  • 仅缩小较大的图像('>')
  • 仅放大较小的图像('<')
  • 填充给定区域('^')
  • 百分比调整大小('%')
  • 像素数限制('@')
根据ImageMagick关于图像几何学处理的文档,几何参数可以是:
scale%              Height and width both scaled by specified percentage 
scale-x%xscale-y%   Height and width individually scaled by specified percent
width               Height automagically selected to preserve aspect ratio 
xheight             Width automagically selected to preserve aspect ratio 
widthxheight        Maximum values of height and width given, ratio preserved
widthxheight^       Minimum values of width and height given, ratio preserved
widthxheight!       Width and height emphatically given, ignore original ratio 
widthxheight>       Change only if an image dimension exceeds a specified dim. 
widthxheight<       Change only if both image dimensions exceed specified dim.

7
您可以使用,:show => '786>x447'来设置固定宽度和比例高度。

1

调整大小选项有限,但您也可以使用纸夹自定义处理器动态调整图像大小。

Railscasts有一个很好的例子,使用纸夹自定义处理器,尽管他的例子允许用户裁剪图像。 http://railscasts.com/episodes/182-cropping-images


0
你可以自行计算高度:
新高度 = 旧高度 * 380 / 旧宽度

我不相信在Paperclip插件的样式选项中实现这一点是可能的。 - Jonathan Soeder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接