Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 340
SnakeWasTheNameTheyGaveMe Avatar asked Jul 02 '11 15:07

SnakeWasTheNameTheyGaveMe


2 Answers

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.

like image 158
vansan Avatar answered Oct 18 '22 02:10

vansan


You should do your own testing on various JPEG compression levels but I've noticed that I can bump ImageMagicks quality setting down to 75 and still not see any noticeable difference - with about a 30-40% file size savings.

My model looks like:

  has_attached_file :photo,
    :styles => {
      :"185x138" => {
        :geometry => "185x138>"
      }    },
    :convert_options => {
      :all => "-auto-orient",
      :"185x138" => "-quality 75",

-quality 75 is for ImageMagick. If you're using a different processor you will need to adjust accordingly.

like image 21
Cody Caughlan Avatar answered Oct 18 '22 03:10

Cody Caughlan