Pages

Friday, December 20, 2013

How to Optimize Image Uploaded via Paperclip

Here is an another gem to reduce the size of the paperclip uploaded image.
    gem "paperclip-compression","~> 0.1.1"

Usage

    class User < ActiveRecord::Base
             has_attached_file :avatar,
              :styles     => { :medium => "300x300>", :thumb => "100x100>" },
              :processors => [:thumbnail, :compression]
    end
we can optimize this code as:

       has_attached_file :attachment, {
        :styles => {
          :medium => ["654x500>", :jpg],
          :thumb =>["200x200#", :jpg]
        },
        :convert_options => {
          :medium => "-quality 80 -interlace Plane",
          :thumb => "-quality 80 -interlace Plane"
          }
        },
         :processors => [:thumbnail, :compression]
After using this optimization trick I am able to save approx 20% of image size in my projects. Pretty nice and clean.

No comments:

Post a Comment