2013-10-25 33 views
1

我正在使用回形針來存儲我的圖像,我想創建一個裁剪/旋轉的圖像作爲縮略圖。這裏是回形針應該運行的正常命令:停止回形針剪裁和其他參數之前調整大小

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的電腦上直接測試了它。然而,在我的服務器上查看日誌,這些參數的順序是不同的。回形針希望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而不是原來的文件(因爲水平填充問題裁剪時)

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 
+0

希望我能幫忙,但我只能推薦使用標準回形針功能來處理這種事情:)如果你想讓我給你一些想法,我可以做一個答案? –

+0

當然,無論幫助,幫助。我甚至可以做一個自定義滾動的處理器,它將覆蓋Paperclip用於生成縮略圖的默認過程,但我不知道從哪裏開始。 – JakeTheSnake

回答

0

我試圖創建自己的回形針處理器,但我遇到了問題。我的處理器重寫了cropper.rb處理器,因爲它們都試圖在縮略圖處理器中使用相同的方法;所以我可以將resize命令移動到參數列表的末尾,但裁剪參數無處可查。我無法弄清楚如何在我的自定義處理器中包含裁剪處理器,整個事情看起來像是一團糟,並且不起作用。

未找到答案。我最終轉向了Carrierwave,並遇到了另一系列問題,我終於找到了解決方案。太糟糕了Carrierwave不記錄圖像處理,但這是一個小的代價。

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

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 

不是最理想的方式,但我得到了它做什麼,我想在不犧牲太多性能。