2

我在一个与回形针 3.0.3 完美配合但与回形针 3.2.0 完全兼容的项目上使用回形针。在 3.0.3 中,它裁剪图像并添加水印,在 3.2.0 中,它在处理每个图像的第一个命令时循环,并一次又一次地执行直到崩溃。

下面找到的代码主要来自 Railcast 关于回形针和我在 stackoverflow 上找到的其他代码。我认为人们可能会感兴趣地看到一个集成了所有这些的工作版本(带有回形针 3.0.3)。

这是模型的代码

require 'paperclip_processors/watermark'
require 'paperclip_processors/cropper'

class Imageblock < ActiveRecord::Base

   belongs_to :block

   attr_accessor :crop_x, :crop_y, :crop_w, :crop_h, :old_size_width, :old_size_height

   validates_presence_of :size_width, :size_height, :photo

   validates_format_of :description, :with => /\A[^"]+\z/, :message => "No quotes allowed"

   has_attached_file :photo, :styles => lambda { |attachment| 
       image = attachment.instance
       dimensions = "#{image.size_width}x#{image.size_height}#"
       { 
          :small => {:geometry => "100x100#"},
          :custom => {:geometry => dimensions, :processors => [:cropper] },
          :large => {:geometry => "500x500>"},
          :display =>{:geometry => "1000x1000>", :watermark_path => "#{Rails.root}/app/assets/images/monogram.png", :processors => [:watermark]},
          :retina =>{:geometry => "2000x2000>", :watermark_path => "#{Rails.root}/app/assets/images/monogram.png", :processors => [:watermark]} 
       }
   }, :dependant => :destroy,  :url => "/art/photo/:id/:style/:basename.:extension", :path => ":rails_root/public/art/photo/:id/:style/:basename.:extension"

   after_update :reprocess_photo, :if => :cropping?

   def cropping?
       !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
   end

   def photo_geometry(style = :original)
       @geometry ||= {}
       @geometry[style] ||= Paperclip::Geometry.from_file(photo.path(style))
   end

   private

   def reprocess_photo
       photo.reprocess!
   end

 end

这是裁剪器处理器的代码

module Paperclip
class Cropper < Thumbnail
   def transformation_command
      if crop_command
         crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ') # super returns     an array like this: ["-resize", "100x", "-crop", "100x100+0+0", "+repage"]
      else
        super
      end
    end

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

水印的代码很长,在stackoverflow中找到的

module Paperclip
class Watermark < Processor
 # Handles watermarking of images that are uploaded.
   attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options, :watermark_path, :overlay, :position

   def initialize file, options = {}, attachment = nil
      super
      geometry          = options[:geometry]
      @file             = file
      @crop             = geometry[-1,1] == '#'
      @target_geometry  = Geometry.parse geometry
      @current_geometry = Geometry.from_file @file
      @convert_options  = options[:convert_options]
      @whiny            = options[:whiny].nil? ? true : options[:whiny]
      @format           = options[:format]
      @watermark_path   = options[:watermark_path]
      @position         = options[:position].nil? ? "SouthEast" : options[:position]
      @overlay          = options[:overlay].nil? ? true : false
      @current_format   = File.extname(@file.path)
      @basename         = File.basename(@file.path, @current_format)
    end

    # TODO: extend watermark

    # Returns true if the +target_geometry+ is meant to crop.
     def crop?
       @crop
     end

     # Returns true if the image is meant to make use of additional convert options.
     def convert_options?
       not @convert_options.blank?
     end

     # Performs the conversion of the +file+ into a watermark. Returns the Tempfile
     # that contains the new image.
     def make
       dst = Tempfile.new([@basename, @format].compact.join("."))
       dst.binmode

       command = "convert"
       params = [fromfile]
       params += transformation_command
       params << tofile(dst)
       begin
         success = Paperclip.run(command, params.flatten.compact.collect{|e| "'#{e}'"}.join(" "))
         rescue PaperclipCommandLineError
         raise PaperclipError, "There was an error resizing and cropping #{@basename}" if @whiny
       end

       if watermark_path
         command = "composite"
         params = %W[-gravity #{@position} #{watermark_path} #{tofile(dst)}]
         params << tofile(dst)
         begin
           success = Paperclip.run(command, params.flatten.compact.collect{|e| "'#{e}'"}.join(" "))
         rescue 
           raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny
         end
       end

       dst
     end

     def fromfile
       File.expand_path(@file.path)
     end

     def tofile(destination)
       File.expand_path(destination.path)
     end

     def transformation_command
       scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
       trans = %W[-resize #{scale}]
       trans += %W[-crop #{crop} +repage] if crop
       trans << convert_options if convert_options?
       trans
     end
  end
end

如果有人知道为什么它不能与回形针 3.2.0 一起使用,我在网上找不到有关此错误的任何帮助。

干杯

4

1 回答 1

0

看看 https://github.com/thoughtbot/paperclip/issues/866 它应该已经修复了。

更新

您的代码将在深度堆栈级别错误中运行。它是通过 after_update 过滤器回调和回形针中的更改引起的(查看问题 866)。你必须移动后处理!从模型调用控制器#update

def update
  if @asset.save
    if params[:asset][:crop_x] 
      @asset.attach.reprocess!
    end
    redirect_to ..
于 2012-12-16T03:40:01.413 回答