Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用回形针在导轨中进行自定义裁剪_Ruby On Rails_Paperclip_Crop - Fatal编程技术网

Ruby on rails 使用回形针在导轨中进行自定义裁剪

Ruby on rails 使用回形针在导轨中进行自定义裁剪,ruby-on-rails,paperclip,crop,Ruby On Rails,Paperclip,Crop,我目前正在使用回形针上传图像并自动生成缩略图。现在我还想添加第二种样式,它使用上传图像中最左边的像素列生成一个像素宽的图像(它也应该与原始图像具有相同的高度)。我将通过CSS使用一个像素宽的图像作为重复背景 是否可以使用曲别针的默认缩略图处理器生成背景图像,或者我需要创建自己的自定义处理器?我已经尝试过创建一个自定义处理器,它将曲别针::处理器子类化,但我不知道如何正确使用曲别针。运行方法。现在,我试图根据Ryan Bate的Railcast在此处创建子类Paperclip::Thumbnail

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

是否可以使用曲别针的默认缩略图处理器生成背景图像,或者我需要创建自己的自定义处理器?我已经尝试过创建一个自定义处理器,它将
曲别针::处理器
子类化,但我不知道如何正确使用
曲别针。运行
方法。现在,我试图根据Ryan Bate的Railcast在此处创建子类
Paperclip::Thumbnail
,但这会引发以下错误:

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
#/app/models/image.rb
类映像{
:thumb=>“32x32#”,
:auto_bg=>{:处理器=>[:autobackground]}
}
结束
#/lib/paperclip\u处理器/Autobackground.rb
模块回形针
类自动背景<缩略图
def转换命令
如果是命令
crop_命令+super.sub(/-crop\S+/,“”)
其他的
超级的
结束
结束
def crop_命令
target=@attachment.instance
如果目标是裁剪?
“-裁剪'1x#{target.height}+0+0'”
结束
结束
结束
结束

我建议您编写一个helper方法,并使用过滤器调用它

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

关于编码风格的另一条评论

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

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


只要有可能,请使用ruby特有的风格…

如果有人感兴趣,我会设法让它工作。在修复这一问题上,对我帮助最大的是Rails调试控制台(我最终开始正确使用它),它允许我更仔细地查看类中的变量,我的
Autobackground
类继承自该类

下面是我所做的:我更改了
:auto_bg
样式,以指向一个可以在处理器中识别的特殊字符串。由于我的处理器是从
曲别针::缩略图
子类化的,因此样式指向的字符串将保存到
@options[:geometry]
。在重写的
transformation\u命令
方法中,我所要做的就是检查
@options[:geometry]
是否设置为特殊的
auto\u bg
字符串,然后运行我的
create\u auto\u bg
方法,而不是让
缩略图
类执行通常的操作。我以前的
create\u auto\u bg
方法没有正确创建创建ImageMagick命令所需的
Thumbnail
字符串数组,因此我重写了它,并使用
@current_geometry
变量来查找原始图像的高度,而不是从Ryan Bate的railscast复制的有故障的
target=@attachment.instance
方法(不确定它在他的代码中是如何工作的)

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

#/app/models/image.rb
类映像{:thumb=>“32x32”,:auto_bg=>“auto_bg”},:处理器=>[:autobackground]
结束
#/lib/paperclip\u处理器/Autobackground.rb
模块回形针
类自动背景<缩略图
def转换命令
如果@options[:geometry]=“auto_bg”
创建\u自动\u背景
其他的
超级的
结束
结束
def创建_自动_背景
#调试器
高度=@current_geometry.height.to_i.to_s
trans=[]
反式
def crop_command
    target = @attachment.instance
    " -crop '1x#{target.height}+0+0'" if target.cropping?
end
#/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