2013-10-24 153 views
2

我遵循jcrop rails教程,但我碰到了一個障礙。事實上,回形針是從原始文件生成縮略圖,但我需要它從另一種樣式生成。原始文件在產品鏡頭和文檔邊緣之間沒有空格。所以我不能進一步收穫。爲了解決這個問題,我製作了另一種具有白色像素填充的風格。這就是我想從中生成縮略圖的原因。Rails回形針從其他風格生成縮略圖

# croppable is the one with the padding...it's what shows up in the crop view. 
# I want :thumb to be generated from THAT style, not :original. 
# When generating from :original, the crop offset/size is screwed because the dimensions of :original don't match :cropped 
# and I can't crop beyond the pixel dimensions of :original. 
has_attached_file :photo, :styles => { 
        :thumb => { :geometry => "300x300#", :format => :jpg, :processors => [:cropper] }, 
        :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 -rotate -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] 

回答

3

我遇到了一個處理器,其他人已經在線並將其用於自己。有關高級使用信息,請參閱http://pjkh.com/articles/speeding-up-thumbnail-generation-with-paperclip/

recursive_thumbnail.rb(LIB中包含/ paperclip_processors)

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 

和模型:

:styles => { 
    :croppable => ["1200x1200>", :jpg], 
    :show => ["x425", :jpg], 
    :general => ["150x375", :jpg], 
    :thumb => { 
     :geometry => "150x150^", 
     :format => :jpg, 
     :processors => [:recursive_thumbnail] }, 
     :thumbnail => :croppable 
    } 
0

也許這會幫助你或別人像我這樣誰谷歌導致這一題。

https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation

生成/再生的縮略圖

注:爲所有 其他樣式 描述了一些下面的例子中會導致斷路徑/網址只有再生的幾個定義樣式之一如果您的paperclip_defaults [:path]中有:hash,並且在您的paperclip_defaults [:hash_data]中有 :updated_at(並且您有 默認值)。除非你真的知道你在做什麼,否則不要這樣做。

您可以使用Paperclip的耙子 任務(重新)生成縮略圖。使用我們上面的示例類:

bundle exec rake paperclip:refresh:thumbnails CLASS=User