If you haven't seen The 15 minute E-Commerce Site CLICK HERE
I'm currently looking for Contract jobs, Contact Me if you are interested. Dave.
Optimize your Images Mar 11
I want to preface this post with a huge thank you to the developers I've had the chance to work with in Belarus (Altoros). Dmitry Ilyashevich and Yury Velikanau wrote the code that ended up in our code base but the whole team in Belarus is great.
Background
A few months ago my boss brought up a service called smush it. Basically this service allows you to upload an image and return an optimized image. Hence making a smaller image that still looks good for its size.
Taking a longer look Yahoo gives you the libraries they use to make smushit work. Take a look at the bottom of the above link(pngcrush && jpegtran). So the thought was that we could simply install these libraries on our server's that we upload pictures to. I wish I had been given this project but Dmitry, Yury and the Altoros team did a great job getting this working.
On to the code. What we did was create a paperclip_postprocess.rb file in config/initializers. Sorry but the code doesn't size well on the blog...
module Paperclip
class Thumbnail < Processor
def make
src = @file
conv = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
conv.binmode
dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
dst.binmode
begin
parameters = []
parameters << source_file_options
parameters << ":source"
parameters << transformation_command
parameters << convert_options
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(conv.path))
if conv.size > 0
format = begin
Paperclip.run("identify", "-format %m :file", :file => "#{File.expand_path(conv.path)}[0]")
rescue PaperclipCommandLineError
""
end
case format.strip
when 'JPEG'
# Part of libjpeg-progs deb package
success = Paperclip.run('jpegtran', "-copy none -optimize -perfect :source > :dest", :source => File.expand_path(conv.path), :dest => File.expand_path(dst.path))
when 'PNG'
success = Paperclip.run('pngcrush', "-rem alla -reduce -brute :source :dest", :source => File.expand_path(conv.path), :dest => File.expand_path(dst.path))
else
dst = conv
end
end
rescue Exception => e
Rails.logger.error "There was an error processing the thumbnail for #{@basename}. Check imagemagick, jpegtran and pngcrush installed."
HoptoadNotifier.notify(
:error_class => "Paperclip - images optimization",
:error_message => "Paperclip ERROR: #{e.message}"
)
end
dst.size > 0 ? dst : conv
end
end
end
At the end of the day This reduced our file sizes dramatically. Some images were reduced in size by about 85%. It is well worth the extra time to get this set up with paperclip.
Check back later as we will try to create our own fork of paperclip and allow this to be a paperclip option. For now install jpegtran and pngcrush on your linux box and start optimizing your images.

17 comments so far
Philip Hallstrom 12 Mar 11
DRH 12 Mar 11
Claudio 12 Mar 11
khelll 13 Mar 11
Rodrigo 21 Mar 11
case format.strip when 'GIF' success = Paperclip.run('gifsicle', "-b -O2 :source", :source => File.expand_path(conv.path)) ...Rodrigo 21 Mar 11
marcus 30 Dec 11
rong 04 Nov 11
marcus 30 Dec 11
DRH 01 Dec 11
DRH 30 Dec 11
DRH 30 Dec 11
Andrea Salicetti 10 Feb 12
Andrea Salicetti 10 Feb 12
test 16 Feb 12
umer 16 Feb 12
Matt 10 Apr 12
Comments are closed