1

客户端.rb

  has_attached_file :avatar,
  :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
  :url => "/system/:attachment/:id/:style/:filename",
  :styles => {:thumb => "144x144#", :grayscale => { :processors => [:grayscale] }}

拇指版效果很好,图像被裁剪到所需的大小,灰度只将该图像转换为灰度,但图像没有被裁剪,这是我在 StackOverflow 上找到的灰度生成器:

库/灰度.rb

module Paperclip
  # Handles grayscale conversion of images that are uploaded.
  class Grayscale < Processor

    def initialize file, options = {}, attachment = nil
      super
      @format = File.extname(@file.path)
      @basename = File.basename(@file.path, @format)
    end

     def make
       src = @file
       dst = Tempfile.new([@basename, @format])
       dst.binmode

       begin
         parameters = []
         parameters << ":source"
         parameters << "-colorspace Gray"
         parameters << ":dest"

         parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")

         success = Paperclip.run("convert", parameters, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
       rescue PaperclipCommandLineError => e
         raise PaperclipError, "There was an error during the grayscale conversion for #{@basename}" if @whiny
       end

       dst
     end
  end
end

要将图像转换为灰度,一些参数将被发送到数组中的 imagemagick,问题是 - 我必须将哪些参数发送到 imagemagick,这样它才能完全"144x144#"执行回形针中的操作。

我尝试跟踪日志以查看"144x144#"日志中的内容,它看起来像这样:-crop '144x144+30+0',我尝试在我的生成器中使用它并将其作为参数发送,例如:

 parameters = []
 parameters << ":source"
 parameters << "-crop '144x144+30+0'"
 parameters << "-colorspace Gray"
 parameters << ":dest"

如果我使用我之前上传的同一张图片,看起来它可以工作,如果我上传另一张图片,则图像被完全错误地裁剪。所以我得出一个结论:-crop '144x144+30+0'由回形针生成的参数是针对特定图像大小的,而对于另一个具有不同大小的图像,通常会发送不同的参数以适应 144 像素。

如何裁剪生成器中的图像以适应144x144#回形针中的等价物,或者我需要发送到 imagemagick 以实现此目的的参数是什么。谢谢你。

4

3 回答 3

3

我决定采用另一种方式,因此我将使用具有所需大小的裁剪文件并使用 imagemagick 将其转换为灰度并在保存模型后将其保存到正确的文件夹中。当我使用系统命令来处理 imagemagick 时,可以删除灰度处理器。

ps这个答案可能有一些缺点,但现在我找不到任何缺点。

客户端.rb

  has_attached_file :avatar,
  :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
  :url => "/system/:attachment/:id/:style/:filename",
  :styles => {:thumb => "144x144#", :grayscale => "144x144#"}

  after_save :convert_grayscale

  def convert_grayscale
    system "convert public/system/avatars/#{self.id}/thumb/#{self.avatar.original_filename} -fx '(r+g+b)/3' public/system/avatars/#{self.id}/grayscale/#{self.avatar.original_filename}"
  end

结果

在此处输入图像描述

于 2013-10-07T07:11:55.043 回答
3

为了简单起见,您可以convert直接附加到,convert_options:

例如

  has_attached_file :file, styles: {
    ir: '640x640>', 
    thumb: '150x150>'
  }

变成:

  has_attached_file :file, styles: {
    ir: {geometry: '640x640>', convert_options: '-colorspace Gray'}, 
    thumb: '150x150>'
  }
于 2015-06-23T16:28:43.063 回答
2

通过反复试验,我找到了一种更简单的方法来做到这一点。使用您找到的灰度处理器代码,将其放入lib/paperclip/grayscale.rb中。

然后,像这样设置你的样式:

:styles => {
  :thumb => "144x144#",
  :grayscale => { :geometry => "144x144#", :processors => [:grayscale, :thumbnail] }
}

这样,灰度样式将由灰度处理器和缩略图处理器使用提供的“几何”进行处理。作为奖励,您将能够使用 Paperclip 的内置 rake 任务来重新处理现有附件。

于 2015-06-01T18:44:03.957 回答