Search code examples
ruby-on-railscompressionpaperclipdeflate

Rails Paperclip image compression compared to what Page Speed produces


I've set up paperclip in rails and everything is working hunky-dory (i actually had to google that...:).

I've noticed however that Page Speed tells me I could losslessly compress my thumbnail and large images (the ones that paperclip produces) further. Is there an option I can put into my model which does this? I've noticed that mod_deflate doesn't compress images (I'm using Firefox).


Solution

  • You can add compression to paperclip processing using the paperclip-compression gem.

    In your Gemfile:

    gem "paperclip-compression", "~> 0.1.1"
    

    (of course run bundle install)

    In your model:

    has_attached_file :avatar,
                    :styles     => { :medium => "300x300>", :thumb => "100x100>" },
                    :processors => [:thumbnail, :compression]
    

    "jpegtran works by rearranging the compressed data (DCT coefficients), without ever fully decoding the image. Therefore, its transformations are lossless"

    Note: if you are running on heroku, you'll need jpegtran, and optipng binaries added to your application. Here's a good article on running binaries on heroku.