Using Paperclip::Processor and RMagick to sharpen my thumbnails


#1

Hi, I’m brand new to RMagick (and pretty new to Rails for that
matter!). Right now, site owners are allowed to upload product and
ingredient photos to the website. Everything works great, ingredients
are sized down and proportioned correctly…the problem is that the
resulting images are so blurry! So I’ve been doing some research and
it looks like Paperclip::Processor may be the way to go. Here is what
I have so far:

*** RAILS_ROOT/lib/paperclip_processors/paperclip_sharpen_image.rb ***

require ‘RMagick’

module Paperclip
class SharpenImage < Paperclip::Processor

    def initialize file, options = {}
        super
        @file             = file
        @format           = options[:format]
        @current_format   = File.extname(@file.path)
        @basename         = File.basename(@file.path,

@current_format)
end

    def make
        src = @file
        image = Magick::Image.read(File.expand_path

(src.path)).first

        image.sharpen(10, 10)!

        # Create Templfile object and write image data to its file
        dst = Tempfile.new([@basename, @format].compact.join("."))
        dst.binmode
        image.write(File.expand_path(dst.path))

        # Return Tempfile object to Paperclip for further handling
        return dst
    end
end

end

*** ingredient.rb ***

has_attached_file :photo, :styles => {:small => “63x75”},
:processors => [:sharpen_image]

But, I’m not having any luck. I just grabbed most of this code from
another site and have been trying to adapt it…but to no avail! Any
help or advice would be greatly appreciated!

Thanks.


#2

Depends what exactly your error is, but I seem to recall RMagick having
issues with TempFile’s default naming convention. The default processor
deals with that. Try subclassing Paperclip::Thumbnail instead?

If not, could you provide more details on the nature of the actual
error?


#3

Matt W. wrote:

        image.sharpen(10, 10)!

“I’m not having any luck” isn’t a very good description of your problem.
However, this is not valid Ruby. It’s possible you meant:

image.sharpen!(10,10)

but there is no Magick::Image.sharpen! method. I think what you want is:

image = image.sharpen(10,10)


#4

I apologize for the poor description. What I should have said is that
nothing appears to happen at all. I don’t see any errors logged, the
thumbnail is still created, but it doesn’t appear to be any different
than before (when I wasn’t using the processor). I appreciate
everyone’s feedback, looks like I’ve got a couple of things I can try.

Thanks.

On Apr 17, 9:10 am, Tim H. removed_email_address@domain.invalid


#5

Matt W. wrote:

Hi, I’m xxxxx new to RMagick (and pretty new to Rails for that
matter!). Right now, site owners are allowed to upload product and
ingredient photos to the website. Everything works great, ingredients
are sized down and proportioned correctly…the problem is that the
resulting images are so blurry!

Magick::Image.resize takes a “support” (a.k.a “sharpen”) argument. See
http://www.imagemagick.org/RMagick/doc/image3.html#resize.


#6

How would I go about utilizing the “support” argument with Paperclip?

On Apr 17, 9:14 am, Tim H. removed_email_address@domain.invalid


#7

It seems that my processor isn’t even being fired. There is no
evidence that it is even being called. I have it in the RAILS_ROOT/lib/
paperclip_processors directory and I’ve double checked the file names.
Does anyone have any idea what I’m doing wrong here?

Thanks!

M@

On Apr 17, 1:19 am, Shaun K. removed_email_address@domain.invalid