Rails Paperclip how to use filter options of ImageMagick?
Asked Answered
M

2

20

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

Mecke answered 14/12, 2010 at 4:36 Comment(0)
M
13

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!

Matins answered 14/12, 2010 at 9:34 Comment(1)
Well feels like it is wayyy easier to add in in the convert options :styles => { :grey => "450x250" }, :convert_options => {:grey => "-blur 0x8"}Pseudo
G
0

Rails 5, Paperclip 5 update

Instead of having to add a library now, you can just call out to ImageMagick's convert command on the system to use its grayscale option. You can do the same for blur or any of the other ImageMagick options, but I needed to do this for conversion to grayscale.

In your model (client that has a logo):

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

Then just use in the view like this (per Paperclips github docs).

In your view:

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

or with a link if you prefer:

<%= link_to(image_tag(client.logo.url(:grayscale), class: 'thumbnail', alt: client.name, title: client.name), client.url ) %>
Gervase answered 13/12, 2016 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.