5

私は:xxx画像プロセッサを持っており、モデルには:bigと:thumbの2つのスタイルがあります。

:xxxで:thumb画像のみを処理し、:big画像をそのままにしておくにはどうすればよいですか?

4

4 に答える 4

19

私は最近同様の問題を抱えていて、メッセージボードでこの解決策を見つけました。それが役に立てば幸い!

has_attached_file :screenshot,
 :default_style => :full,
 :styles => {
   :full => "280x210",
   :cropped => { :processors => [:screenshot_crop] }
 }
于 2010-07-14T17:43:39.190 に答える
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

Paperclip 2.3.1.1 でテスト済み

Paperclip 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 ファイルに移動するだけです。

于 2010-08-26T15:35:55.303 に答える
1

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

RakefileAttachmentクラスを見て、特定のサムネイル サイズを指定できるように変更することもできますが、現在の設計では、オリジナルを取得してオリジナルからすべてのサムネイルをやり直すことを前提としています。

于 2009-07-23T01:46:44.493 に答える
0

エレガントではありませんが、うまくいきました。

スタイルの 1 つは、他のすべてのスタイルとは異なる寸法にする必要があります。このようにして、カスタム ペーパークリップ プロセッサで、コマンド文字列の内容に指定された寸法が含まれているかどうかを確認できます。もしそうならあなたは特別な処理をします、そうでなければあなたはしません。

(Ryan Bate の Railscast Episode 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

私の状況では、最終結果が何も変わらなかったので、特別でない場合でも再処理しても問題ありませんでした。

于 2010-05-19T18:02:35.377 に答える