Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Paperclip how to use filter options of ImageMagick?

Tags:

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'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:
alt text

To this:
alt text

like image 312
jyoseph Avatar asked Dec 14 '10 04:12

jyoseph


1 Answers

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

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

Have a look at https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/thumbnail.rb

I personnaly use my own processor.

In Model:

  has_attached_file :logo,
                    :url  => PaperclipAssetsController.config_url,
                    :path => PaperclipAssetsController.config_path,
                    :styles => {
                                 :grayscale => { :processors => [:grayscale] }
                               }

In 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

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

like image 185
plang Avatar answered Oct 02 '22 05:10

plang