2009-07-22 27 views

回答

1

默認情況下,Rake任務刷新所有縮略圖。請記住,它不會觸摸/處理原始圖像。

你可以有一個look at the RakefileAttachment類和修改,以允許你指定一個特定的縮略圖大小,但目前的設計假設你想要原始的原始和重做所有縮略圖。

+0

是否還有其他選項僅裁剪:拇指圖像而讓其他人獨自一人?我的意思是不修改附件類? 也許模型/控制器中有一些額外的代碼? – astropanic 2009-07-23 07:07:15

0

我克服了這一點 - 這不是優雅的,但它對我有效。

您的一個樣式應該具有與所有其他樣式不同的尺寸。這樣,在您的自定義回形針處理器中,您可以查看命令字符串的內容是否包含給定的尺寸。如果是這樣,你會做特殊處理,否則,你不會。

(我修剪這些代碼 - 而修改了它 - 從瑞安貝特的Railscast集182)

module Paperclip 
    class Cropper < Thumbnail 
    def transformation_command 
     SPECIAL_PROCESSING_FLAG = "150x150" 
     if crop_command && super.include?(SPECIAL_PROCESSING_FLAG) 
     crop_command + super.sub(/ -crop \S+/, '') 
     else 
     super 'do nothing fancy 
     end 
    end 

    def crop_command 
     target = @attachment.instance 
     if target.cropping? 
     " -crop '#{target.crop_w.to_i}x#{target.crop_h.to_i}+#{target.crop_x.to_i}+#{target.crop_y.to_i}'" 
     end 
    end 
    end 
end 

在我的情況它沒有問題,我們在非特殊情況下重新處理過,因爲最終的結果沒有改變。

18

我最近有一個類似的問題,並在留言板上找到了這個解決方案。希望能幫助到你!

has_attached_file :screenshot, 
:default_style => :full, 
:styles => { 
    :full => "280x210", 
    :cropped => { :processors => [:screenshot_crop] } 
} 
+0

感謝您的提示!看來你應該提供`:geometry`參數 - `:cropped => {:geometry =>'whatever',:processors => [:screenshot_crop]}`否則它給出'undefined method \`[]'nil: NilClass`。 – jibiel 2012-04-18 09:59:13

1

將此代碼添加到您的paperclip.rake文件:

desc "Reprocesses your attachments style (set CLASS, ATTACHMENT and STYLE)" 
    task :style => :environment do 
     module JustForOneDay 
     NAME = ENV['STYLE'] 
     end 

     module ::Paperclip 
     class Attachment 
      def post_process_styles #:nodoc: 
      @styles.each do |name, args| 
       if JustForOneDay::NAME == name 
       begin 
        raise RuntimeError.new("Style #{name} has no processors defined.") if args[:processors].blank? 
        @queued_for_write[name] = args[:processors].inject(@queued_for_write[:original]) do |file, processor| 
        Paperclip.processor(processor).make(file, args, self) 
        end 
       rescue PaperclipError => e 
        log("An error was received while processing: #{e.inspect}") 
        (@errors[:processing] ||= []) << e.message if @whiny 
       end 
       end 
      end 
      end 
     end 
     end 

     for_all_attachments do |instance, name| 
     result = instance.send(name).reprocess! 
     end 
    end 
    end 

用回形針測試2.3.1.1

在回形針2.3.3這應該是:

def post_process_styles #:nodoc: 
    styles.each do |name, style| 
    if JustForOneDay::NAME == name 
    begin 
     raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank? 
     @queued_for_write[name] = style.processors.inject(@queued_for_write[:original]) do |file, processor| 
     Paperclip.processor(processor).make(file, style.processor_options, self) 
     end 
    rescue PaperclipError => e 
     log("An error was received while processing: #{e.inspect}") 
     (@errors[:processing] ||= []) << e.message if @whiny 
    end 
    end 
    end 
end 

很簡單,只需在回形針版本中找到attachment.rb文件即可。