Rails 5,Paperclip 5 更新
现在不需要再添加库,只需在系统上调用ImageMagick的转换命令来使用其灰度选项。您也可以使用相同的方法进行模糊或其他任何ImageMagick选项,但我需要使用它将图像转换为灰度。
在您的模型中(具有徽标的客户端):
class Client < ApplicationRecord
has_attached_file :logo,
styles: { thumb: "243x243#", grayscale: "243x243#" }
validates_attachment_content_type :logo, content_type: /\Aimage\/.*\z/
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 ) %>
:styles => { :grey => "450x250" }, :convert_options => {:grey => "-blur 0x8"}
- Ben