Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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 回形针图像Magick转换为灰度并裁剪以适合144x144#_Ruby On Rails_Ruby_Imagemagick_Paperclip - Fatal编程技术网

Ruby on rails 回形针图像Magick转换为灰度并裁剪以适合144x144#

Ruby on rails 回形针图像Magick转换为灰度并裁剪以适合144x144#,ruby-on-rails,ruby,imagemagick,paperclip,Ruby On Rails,Ruby,Imagemagick,Paperclip,客户端.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 => [:

客户端.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] }}
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
  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
thumb版本工作得很好,图像被裁剪到所需大小,灰度仅将该图像转换为灰度,但图像未被裁剪,以下是我在StackOverflow上找到的灰度生成器:

lib/grayscale.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] }}
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
  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
如果我使用了之前上传的同一张图片,看起来效果不错,如果我上传了另一张图片,那么图片的裁剪就完全错误了。因此,我得出结论,回形针生成的参数:
-crop'144x144+30+0'
用于特定的图像大小,而对于另一个具有不同大小的图像,通常会发送不同的参数以适合144px


如何在生成器中裁剪图像,使其与回形针中的
144x144 
等效,或者需要向imagemagick发送哪些参数才能实现此目的。谢谢。

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

p、 这个答案可能有一些缺点,但目前我找不到

客户端.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] }}
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
  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

结果


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

p、 这个答案可能有一些缺点,但目前我找不到

客户端.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] }}
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
  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

结果


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

然后,按如下方式设置样式:

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

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

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

然后,按如下方式设置样式:

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

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

为了简化操作,您可以使用,
convert\u选项直接附加到
convert

e、 g

变成:

  has_attached_file :file, styles: {
    ir: {geometry: '640x640>', convert_options: '-colorspace Gray'}, 
    thumb: '150x150>'
  }

为了简化操作,您可以使用,
convert\u选项:

e、 g

变成:

  has_attached_file :file, styles: {
    ir: {geometry: '640x640>', convert_options: '-colorspace Gray'}, 
    thumb: '150x150>'
  }

这是迄今为止最干净的选择。谢谢你,汤姆邓宁!这是迄今为止最干净的选择。谢谢你,汤姆邓宁!