Ruby on rails Rails回形针如何使用ImageMagick的过滤器选项?
我最近用Rails实现了回形针,并想试用ImageMagick中的一些过滤器选项,例如。我还没有找到任何关于如何做到这一点的例子。它是否通过:样式作为另一个选项Ruby on rails Rails回形针如何使用ImageMagick的过滤器选项?,ruby-on-rails,imagemagick,paperclip,Ruby On Rails,Imagemagick,Paperclip,我最近用Rails实现了回形针,并想试用ImageMagick中的一些过滤器选项,例如。我还没有找到任何关于如何做到这一点的例子。它是否通过:样式作为另一个选项 :styles => { :medium => "300x300#", :thumb => "100x100#" } @普朗格的回答是正确的,但我想给出模糊的确切解决方案,以防有人看到并发现以下问题: :convert_options => { :all => "-blur 0x8" } // -blu
:styles => { :medium => "300x300#", :thumb => "100x100#" }
@普朗格的回答是正确的,但我想给出模糊的确切解决方案,以防有人看到并发现以下问题:
:convert_options => { :all => "-blur 0x8" }
// -blur {radius}x{sigma}
它改变了这一点:对此:
我没有对此进行测试,但您应该能够使用“convert\u options”参数,如下所示:
:convert_options => { :all => ‘-colorspace Gray’ }
看看
我个人使用自己的处理器
在模型中:
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
模块回形针
#处理上载图像的灰度转换。
类灰度处理器
def初始化文件,选项={},附件=nil
超级的
@格式=File.extname(@File.path)
@basename=File.basename(@File.path,@format)
结束
def制造
src=@file
dst=Tempfile.new([@basename,@format])
dst.binmode
开始
参数=[]
参数e
raise papercliperor如果@whiny,“在#{@basename}的灰度转换过程中出现错误”
结束
dst
结束
结束
结束
对于一个简单的灰度转换来说,这可能不是100%必需的,但它可以工作 Rails 5、回形针5更新 您不必现在就添加库,只需在系统上调用以使用其。您可以对模糊或任何其他ImageMagick选项执行相同的操作,但我需要对转换为灰度进行此操作 在您的模型中(具有徽标的客户端):
类客户端
然后只需在视图中这样使用(per)
在你看来:
<%= 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 ) %>
感觉在convert选项
:style=>{:grey=>“450x250”},:convert\u options=>{:grey=>“-blur 0x8”}中添加更容易
<%= link_to(image_tag(client.logo.url(:grayscale), class: 'thumbnail', alt: client.name, title: client.name), client.url ) %>