【问题标题】:Custom cropping in Rails with Paperclip在 Rails 中使用 Paperclip 自定义裁剪
【发布时间】:2011-04-25 03:31:27
【问题描述】:

我目前正在使用 Paperclip 上传图片并自动生成缩略图。现在我还想添加第二种样式,它使用上传图像中最左边的像素列生成一个像素宽的图像(它也应该与原始图像具有相同的高度)。我将通过 CSS 使用一个像素宽的图像作为重复背景。

是否可以使用 Paperclip 的默认缩略图处理器生成该背景图像,还是我需要创建自己的自定义处理器?我已经尝试创建一个子类Paperclip::Processor 的自定义处理器,但我不明白如何正确使用Paperclip.run 方法。现在我正在尝试根据 Ryan Bate 的 Railcast 将 Paperclip::Thumbnail 子类化:http://railscasts.com/episodes/182-cropping-images,但这会引发此错误:

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]):
app/controllers/images_controller.rb:11:in `create'

images_controller.rb 的第 11 行:

@image = @review.images.build(params[:image])

如果我不尝试使用 Autobackground 自定义处理器,images_controller.rb 的第 11 行工作正常,所以错误一定是处理器中的代码。

到目前为止,这是我的代码:

#/app/models/image.rb
class Image < ActiveRecord::Base
   belongs_to :review

   has_attached_file :image, :styles => {
      :thumb => "32x32#",
      :auto_bg => { :processors => [:autobackground] }
   }
end

#/lib/paperclip_processors/Autobackground.rb
module Paperclip
   class Autobackground < Thumbnail
      def transformation_command
         if crop_command
            crop_command + super.sub(/ -crop \S+/, '')
         else
            super
         end
      end

      def crop_command
         target = @attachment.instance
         if target.cropping?
            " -crop '1x#{target.height}+0+0'"
         end
      end
   end
end

【问题讨论】:

    标签: ruby-on-rails paperclip crop


    【解决方案1】:

    如果有人感兴趣,我设法让这个工作。在解决这个问题时真正帮助我最大的一件事是 Rails 调试控制台(我终于开始正确使用它),它让我能够更仔细地查看我的 Autobackground 类继承的 Paperclip::Thumbnail 类中的变量来自。

    这就是我所做的:我更改了:auto_bg 样式以指向一个我可以在我的处理器中识别的特殊字符串。由于我的处理器是Paperclip::Thumbnail 的子类,因此样式指向的字符串将保存到@options[:geometry]。在被覆盖的transformation_command 方法中,我所要做的就是检查@options[:geometry] 是否设置为特殊的auto_bg 字符串,然后运行我的create_auto_bg 方法,而不是让Thumbnail 类做这通常的事情。我的旧create_auto_bg 方法没有正确创建Thumbnail 需要创建ImageMagick 命令的字符串数组,所以我重写了它并使用@current_geometry 变量来查找原始图像的高度而不是错误的@我从 Ryan Bate 的 railscast 复制的 987654334@ 方法(不确定它在他的代码中是如何工作的)。

    我确信有一个更优雅的解决方案,但我对 Ruby 和 RoR 还是很陌生,所以现在必须这样做。我希望这可以帮助任何面临类似挑战的人:)

    #/app/models/image.rb
    class Image < ActiveRecord::Base
       belongs_to :review
       has_attached_file :image, :styles => { :thumb => "32x32#", :auto_bg => "auto_bg" }, :processors => [:autobackground]
    end
    
    #/lib/paperclip_processors/Autobackground.rb
    module Paperclip
       class Autobackground < Thumbnail
          def transformation_command
             if @options[:geometry] == "auto_bg"
                create_auto_bg
             else
                super
             end
          end
    
          def create_auto_bg
             #debugger
             height = @current_geometry.height.to_i.to_s
             trans = []
             trans << "-crop" << "1x#{height}+0+0"
             trans
          end
       end
    end
    

    【讨论】:

      【解决方案2】:

      我建议你编写一个辅助方法并使用过滤器调用它...

      有几种工具可以为你发挥这种魔力......

      关于编码风格的更多评论...

      我更喜欢写 ruby​​ 风格的代码,比如

      def crop_command
          target = @attachment.instance
          if target.cropping?
              " -crop '1x#{target.height}+0+0'"
          end
      end
      

      到

      def crop_command
          target = @attachment.instance
          " -crop '1x#{target.height}+0+0'" if target.cropping?
      end
      

      只要有可能,请使用 ruby​​ 特定的样式...

      【讨论】:

        猜你喜欢
        • 2011-12-28
        • 2012-12-30
        • 2010-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-05
        • 2020-12-15
        • 2017-06-14
        相关资源
        最近更新 更多