使用Paperclip/Rails将竖向图片转为横向,并在新图片左右添加填充。

3

我正在使用Paperclip和Rails。目前,如果用户上传肖像图像,Paperclip会裁剪图像的顶部和底部,并“强制”中间部分适应我的定义样式(如下所示)。

然而,我想要的是保留肖像图像,并在图像左右添加“间距”。基本上,在新的横向图像内保留肖像图像。到目前为止,我只能找到简单旋转图像的示例。请参见以下示例:

enter image description here

enter image description here

enter image description here

这是我的样式信息:

has_attached_file :image,
                  :styles => { thumb: "100x100#",
                               medium: "300x300>",
                               display: "759x506#" }

目前我没有应用任何预处理、后处理或插值。

1个回答

3
根据ImageMagick文档,您可以使用-extent选项来实现此目的,例如:
convert input.jpg -resize 800x600 -background black -compose Copy \ 
-gravity center -extent 800x600 -quality 92 output.

根据Paperclip文档,您可以按照以下方式为paperclip添加相关的命令行标志。例如,对于:medium

(文档链接)

has_attached_file :image, :styles => { thumb: "100x100#",
                                       medium: "300x300>",
                                       display: "759x506#" },
                          :convert_options => { all: "-background black -compose Copy -gravity center"
                                                medium:  "-extent 300x300"}

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