Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reduce the quality of an uploading image using Paperclip?

I am running Ruby on Rails 3 and I would like to reduce the quality of an uploading image using the Paperclip plugin/gem. How can I do that?


At this time in my model file I have:

  has_attached_file :avatar, 
    :styles      => {
      :thumb     => ["50x50#",   :jpg],
      :medium    => ["250x250#", :jpg],
      :original  => ["600x600#", :jpg] }

that will convert images in to the .jpg format and will set dimensions.

like image 881
user502052 Avatar asked Feb 14 '11 03:02

user502052


3 Answers

Try using convert_options.

has_attached_file :avatar, 
                  :styles          => { :thumb => '50x50#' },
                  :convert_options => { :thumb => '-quality 80' }
like image 158
James Avatar answered Nov 12 '22 16:11

James


From the paperclip wiki, there's an option for quality:

class User < ActiveRecord::Base
  has_attached_file :photo,
                    :styles => {
                      :small => {
                        :geometry => '38x38#',
                        :quality => 40,
                        :format => 'JPG'
                      },
                      :medium => {
                        :geometry => '92x92#',
                        :quality => 50
                      }
end
like image 42
Peter Brown Avatar answered Nov 12 '22 16:11

Peter Brown


As James says, once you figure out the correct arguments to pass to ImageMagick's convert by experimenting on the command line, you can pass these in to Paperclip through the convert_options option as in James' example.

If you have multiple arguments, pass them in as an array. Here's an example which I laboured over for a while:

:convert_options => {:medium => ["-shave", "2x2", "-background", "white", 
                                 "-gravity", "center", "-extent", 
                                 "530x322", "+repage"],
                     :small  => ["-shave", "1x1"] }
like image 28
edavey Avatar answered Nov 12 '22 15:11

edavey