2009-07-22 7 views

答えて

1

デフォルトでは、Rakeタスクはすべてのサムネイルを更新します。元の画像には触れない/処理しないことに注意してください。

look at the RakefileクラスとAttachmentクラスがあり、特定のサムネイルサイズを指定できるように修正することができますが、現在のデザインでは元のものからすべてのサムネイルを元に戻したいと仮定しています。

+0

他にも、親指の画像だけをトリミングするオプションはありますか? Attachmentクラスを変更せずに モデル/コントローラーに余分なコードが含まれていますか? – astropanic

0

私はこれを凝縮しました - それはエレガントではありませんが、それは私のために働いていました。

あなたのスタイルの1つは、他のすべてのスタイルとは異なる寸法を持つ必要があります。このように、カスタムPaperclip Processorでは、コマンド文字列の内容に指定された次元が含まれているかどうかを確認できます。もしそうなら特別な処理を行い、そうでなければあなたはそうしないでしょう。

(私はこのコードをクリッピング - 、それを修正 - ライアンベイトの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 => {:geometry => 'whatever'、:processors => [:screenshot_crop]} 'を指定しなければならないようです。そうでなければ、nilに対して' '未定義のメソッド\' []' NilClass'。 – jibiel

1

あなたpaperclip.rakeファイルにこのコードを追加します。

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 

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で、これはあるべきペーパークリップでテスト簡単ですが、paperclipバージョンのattachment.rbファイルに移動してください。