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

4

6

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.

Flyte answered 14/2, 2011 at 3:9 Comment(0)
M
16

Try using convert_options.

has_attached_file :avatar, 
                  :styles          => { :thumb => '50x50#' },
                  :convert_options => { :thumb => '-quality 80' }
Megasporophyll answered 14/2, 2011 at 4:2 Comment(0)
F
11

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
Franciscafranciscan answered 14/2, 2011 at 3:20 Comment(3)
I made like that, but it seams don't work: Paperclip doesn't reduce the image quality! I tryed also to restart the server.Flyte
It worked for me. Make sure, if you're in a rails console and running the reprocessing, to exit and run rails console again, because otherwise it won't pick up the model changes.Thyroiditis
This solution appears to be deprecated. The wiki link cited as the source is now broken, and my effort to implement it this way did not work.Hudgens
J
3

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"] }
Jobey answered 14/2, 2011 at 9:30 Comment(0)
J
0

Except -quality, the -strip option of ImageMagick can remove all profile and other fluff from the image which may reduce more size

has_attached_file :photo,
  :styles => {
  :thumb => "100x100#" },
  :convert_options => {
  :thumb => "-quality 75 -strip" }
Justification answered 28/2, 2017 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.