5

我有一个:xxx 图像处理器,模型中有两种样式:big 和 :thumb。

我如何处理 :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

默认情况下,Rake 任务会刷新所有缩略图。请记住,它不会触摸/处理原始图像。

您可以查看 RakefileAttachment类并进行修改以允许您指定特定的缩略图大小,但当前的设计假设您想要获取原始文件并重做原始文件中的所有缩略图。

于 2009-07-23T01:46:44.493 回答
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 测试

在 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

这很简单,只需转到您的回形针版本中的附件.rb 文件。

于 2010-08-26T15:35:55.303 回答
0

我把这个弄混了——它并不优雅,但它对我有用。

您的一种样式应具有与所有其他样式不同的尺寸。这样,在您的自定义回形针处理器中,您可以查看命令字符串的内容是否包含给定的尺寸。如果是这样你会做特殊处理,如果不是,你不会。

(我从 Ryan Bate 的 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

在我的情况下,我们在非特殊情况下重新处理也没关系,因为最终结果没有改变。

于 2010-05-19T18:02:35.377 回答