Rails Paperclip如何使用ImageMagick的过滤器选项? [英] Rails Paperclip how to use filter options of ImageMagick?

查看:105
本文介绍了Rails Paperclip如何使用ImageMagick的过滤器选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用Rails实现了Paperclip,并希望尝试使用ImageMagick中的一些过滤器选项,例如 blur 。我无法找到任何如何做到这一点的例子。它是否被传递:样式作为另一种选择?

I recently implemented Paperclip with Rails and want to try out some of the filter options from ImageMagick such as blur. I've not been able to find any examples of how to do this. Does it get passed through :style as another option?

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






@plang的回答是正确的但我想给出模糊的确切解决方案,以防万一有人在寻找并发现这个问题:


@plang's answer was correct but I wanted to give the exact solution to the blur, just in case someone was looking and found this question:

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

这改变了这个:

Which changed this:

对此:

To this:

推荐答案

我没有对此进行测试,但您应该可以使用convert_options参数,如下所示:

I did not test this, but you should be able to use the "convert_options" parameter, like this:

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

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

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

I personnaly use my own processor.

模型:

  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%的必要转换,但它的工作原理!

This might not be 100% necessary for a simple grayscale conversion, but it works!

这篇关于Rails Paperclip如何使用ImageMagick的过滤器选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆