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

荒凉一梦 提交于 2019-12-01 18:05:00

Try using convert_options.

has_attached_file :avatar, 
                  :styles          => { :thumb => '50x50#' },
                  :convert_options => { :thumb => '-quality 80' }
Peter Brown

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

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"] }

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" }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!