在其他参数之前停止回形剪裁和调整大小

时间:2013-10-25 04:31:02

标签: ruby-on-rails ruby-on-rails-3 imagemagick paperclip

我正在使用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>"命令置为FIRST,然后将-crop 433x433+69+88置为SECOND,然后将其余的参数放入其中。这会改变最终图像的外观!不是我想要的。这是它在日志中输出的内容:

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 - 用于运行:thumb thumbnail生成:croppable而不是原始文件(因为裁剪时出现水平填充问题)

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

cropper.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

2 个答案:

答案 0 :(得分:0)

我尝试创建自己的回形针处理器,但我遇到了问题。我的处理器覆盖了cropper.rb处理器,因为他们都试图在Thumbnail处理器中使用相同的方法;所以我可以将resize命令移动到参数列表的末尾,但庄稼参数无处可寻。我无法弄清楚如何在我的自定义处理器中包含裁剪处理器,整个事情看起来像是一团糟而且无法正常工作。

找不到答案。我最终转向Carrierwave并遇到另一组问题,我终于找到了解决方案。太糟糕Carrierwave没有记录图像的处理,但这是一个很小的代价。

Carrierwave RMagick not removing transparency in convert to jpg
Carrierwave +repage option not working

答案 1 :(得分:0)

遇到同样的问题并意识到我无法改变裁剪和调整大小的顺序。但我确实设法做了一个小小的黑客,让我得到了我想要的结果。我在模型上创建了另一个图像附件。

has_mongoid_attached_file :cropped_image, {:styles => :original => '1920x1680>', :processors => [:cropper]}

仅使用原始尺寸裁剪图像。然后另一个图像将是:

has_mongoid_attached_file :image, {:styles => IMAGE_STYLES, :processors => [:thumbnail]}

那个将拥有所有不同大小的缩略图,并将完成所有大小调整。所以在播种之后,我会做这样的事情:

self.image = self.cropped_image
self.save

不是最理想的方式,但我在不牺牲太多性能的情况下做到了我想做的事。