我正在使用 Paperclip 来存储我的图像,并且我想创建一个裁剪/旋转的图像作为缩略图。这是 Paperclip 应该运行的 PROPER 命令:
convert [file].jpg -gravity center -distort SRT -30 -quality 100 -antialias -flatten -background white -unsharp 0.3x0.3+5+0 -crop 433x433+69+88 +repage -resize "300x300>" [file].jpg
这会产生我想要的结果。我已经在安装了 imagemagick 的电脑上直接对其进行了测试。但是查看我服务器上的日志,这些参数的顺序是不同的。Paperclip 想要 a) 将-resize "300x300>"
命令放在第一个,然后放在-crop 433x433+69+88
第二个,然后将其余的参数放在后面。这会改变最终图像的外观!不是我想要的。这是它在日志中输出的内容:
convert [file].jpg -auto-orient -resize "300x300>" -crop 433x433+69+88 +repage -gravity center -distort SRT -30 -quality 100 -antialias -flatten -background white -unsharp 0.3x0.3+5+0 [file].jpg
...这是我模型中的配置:
Wine.rb
has_attached_file :photo, :styles => {
:thumb => {
:geometry => "300x300>",
:format => :jpg,
:processors => [:cropper, :recursive_thumbnail],
:thumbnail => :croppable
},
:general => ["150x375", :jpg],
:show => ["x425", :jpg],
:croppable => ["1200x1200>", :jpg]
},
:url => "/assets/wines/:style/:wine_name",
:path => ":rails_root/public:url",
:default_url => ":wine_default",
:default_path => ":rails_root/public:wine_default",
:default_style => :show,
:convert_options => {
:thumb => '-gravity center -distort SRT -30',
:croppable => '-gravity center -extent 1200x1200',
:general => '-gravity center -extent 150x375 -quality 95',
:all => '-quality 100 -antialias -flatten -background white -unsharp 0.3x0.3+5+0'
},
:processors => [:thumbnail, :compression]
基本上它按以下顺序运行 convert.exe:[:geometry][:transformations][:convert_options]。
我如何按我想要的顺序得到东西?
recursive_thumbnail.rb - 用于从 :croppable 而不是原始文件运行 :thumb 缩略图生成(因为裁剪时的水平填充问题)
module Paperclip
class RecursiveThumbnail < Thumbnail
def initialize file, options = {}, attachment = nil
super Paperclip.io_adapters.for(attachment.styles[options[:thumbnail] || :original]), options, attachment
end
end
end
裁剪器.rb
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
super.join(' ').sub(/ -crop \S+/, '').split(' ') + crop_command
else
super
end
end
def crop_command
target = @attachment.instance
if target.cropping?
["+repage", "-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}", "+repage"]
end
end
end
end