如何在Rails Paperclip中使用ImageMagick的过滤选项?

20

我最近在Rails中使用了Paperclip,并想尝试一些来自ImageMagick的过滤选项,例如模糊。我找不到如何操作的示例。它是否作为另一个选项通过:style传递呢?

:styles => { :medium => "300x300#", :thumb => "100x100#" }

@plang的答案是正确的,但是我想给出模糊的确切解决方案,以防有人在查看此问题时找到它:

:convert_options => { :all => "-blur 0x8" }
// -blur  {radius}x{sigma} 

这将原本的:
alt text

转化为了:
alt text

2个回答

13

我没有测试过,但你应该能够使用"convert_options"参数,像这样:

:convert_options => { :all => ‘-colorspace Gray’ }

请查看https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/thumbnail.rb

我个人使用自己的处理器。

在模型中:

  has_attached_file :logo,
                    :url  => PaperclipAssetsController.config_url,
                    :path => PaperclipAssetsController.config_path,
                    :styles => {
                                 :grayscale => { :processors => [:grayscale] }
                               }
在lib中:
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

对于简单的灰度转换来说,这可能并不是100%必要的,但它是起作用的!


4
感觉在转换选项中添加非常容易: :styles => { :grey => "450x250" }, :convert_options => {:grey => "-blur 0x8"} - Ben

0

Rails 5,Paperclip 5 更新

现在不需要再添加库,只需在系统上调用ImageMagick的转换命令来使用其灰度选项。您也可以使用相同的方法进行模糊或其他任何ImageMagick选项,但我需要使用它将图像转换为灰度。

在您的模型中(具有徽标的客户端):

class Client < ApplicationRecord
  has_attached_file :logo,
                    styles: { thumb: "243x243#", grayscale: "243x243#" }
  # ensure it's an image
  validates_attachment_content_type :logo, content_type: /\Aimage\/.*\z/

  # optional, just for name and url to be required
  validates :name, presence: true
  validates :url, presence: true

  after_save :convert_grayscale

  def convert_grayscale
    system "convert #{self.logo.path(:thumb)} -grayscale Rec709Luminance #{self.logo.path(:grayscale)}"
  end

  def logo_attached?
    self.logo.file?
  end
end

然后在视图中像这样使用(参见{{link1:Paperclips github文档}})。

在您的视图中:

<%= image_tag(client.logo.url(:grayscale), class: 'thumbnail', alt: client.name, title: client.name) %>

或者如果您喜欢,可以附带链接:

<%= link_to(image_tag(client.logo.url(:grayscale), class: 'thumbnail', alt: client.name, title: client.name), client.url ) %>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接