Ruby on rails 回形针在选定样式上运行处理器

Ruby on rails 回形针在选定样式上运行处理器,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,我有一个:xxx图像处理器,模型中有两种样式:大的和:拇指 如何处理:xxx仅处理:thumb图像而不处理:big图像?默认情况下,Rake任务刷新所有缩略图。请记住,它不会接触/处理原始图像 您可以使用和类,并进行修改,以允许您指定特定的缩略图大小,但当前的设计假定您希望获取原始缩略图并从原始缩略图中重做所有缩略图。我对此进行了修改--这并不优雅,但对我来说很有用 其中一种样式的尺寸应不同于所有其他样式。这样,在自定义曲别针处理器中,可以查看命令字符串的内容是否包含给定的尺寸。如果是这样,你会

我有一个:xxx图像处理器,模型中有两种样式:大的和:拇指


如何处理:xxx仅处理:thumb图像而不处理:big图像?

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

您可以使用和类,并进行修改,以允许您指定特定的缩略图大小,但当前的设计假定您希望获取原始缩略图并从原始缩略图中重做所有缩略图。

我对此进行了修改--这并不优雅,但对我来说很有用

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

(我从Ryan Bate的Railscast第182集剪辑并修改了这段代码。)

模块回形针
类裁剪器<缩略图
def转换命令
特殊处理标志=“150x150”
如果裁剪命令和超级包含?(特殊处理标志)
crop_命令+super.sub(/-crop\S+/,“”)
其他的
超级“不做任何花哨的事”
结束
结束
def crop_命令
target=@attachment.instance
如果目标是裁剪?
“-crop'{target.crop{w.to_i}x{target.crop{h.to_i}+{target.crop{ux.to_i}+{target.crop{y.to_i}”
结束
结束
结束
结束

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

我最近遇到了类似的问题,并在留言板上找到了这个解决方案。希望有帮助

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

将以下代码添加到您的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
desc“重新处理附件样式(设置类、附件和样式)”
任务:style=>:环境执行
模块仅限一天
NAME=ENV['STYLE']
结束
模块::回形针
班级附件
def post_process#U styles#:nodoc:
@styles.each do| name,args|
如果只是一天::NAME==NAME
开始
raise RUNTIMERROR.new(“Style#{name}未定义处理器。”)如果args[:processors]。是否为空?
@排队等待写入[name]=args[:处理器]。注入(@queued_for_write[:original])do |文件,处理器|
回形针.处理器(处理器).make(文件,参数,自)
结束
营救纸牌运输机=>e
日志(“处理时收到错误:#{e.inspect}”)
(@errors[:processing]| |=[])e
日志(“处理时收到错误:#{e.inspect}”)

(@errors[:processing]| |=[])是否有其他选项仅裁剪:拇指图像而不裁剪其他图像?我的意思是不修改附件类?也许模型/控制器中有一些额外的代码?谢谢你的提示!似乎您应该提供
:geometry
参数-
:crapped=>{:geometry=>'whatever',:processors=>[:screenshot\u crapp]}
否则它会为nil:NilClass提供
未定义的方法“[]”。
   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
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