首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用回形针在Rails中自定义裁剪

使用回形针在Rails中自定义裁剪
EN

Stack Overflow用户
提问于 2010-10-14 07:56:38
回答 2查看 3.4K关注 0票数 2

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

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

代码语言:javascript
复制
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行:

代码语言:javascript
复制
@image = @review.images.build(params[:image])

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

到目前为止,我的代码如下:

代码语言:javascript
复制
#/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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-10-15 02:34:34

如果有人感兴趣,我设法让它正常工作。在修复这个问题上,真正对我帮助最大的是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中复制的有问题的target = @attachment.instance方法(不确定它在他的代码中是如何工作的)。

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

代码语言:javascript
复制
#/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
EN

Stack Overflow用户

发布于 2010-10-14 15:21:29

我建议你写一个帮助器方法,并用过滤器调用它……

有几个可用的工具可以为你做这个魔术…

还有一条关于编码风格的评论...

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

代码语言:javascript
复制
def crop_command
    target = @attachment.instance
    if target.cropping?
        " -crop '1x#{target.height}+0+0'"
    end
end

代码语言:javascript
复制
def crop_command
    target = @attachment.instance
    " -crop '1x#{target.height}+0+0'" if target.cropping?
end

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3928996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档