开发者

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 not开发者_StackOverflowiced that mod_deflate doesn't compress images (I'm using Firefox).


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.


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.


What about FFMPEG or AVCONV?

sudo apt-get install ffmpeg/avconv

= initializer

Paperclip.options[:command_path] = "/usr/bin/" # see `which ffmpeg`

= Modal

after_save :compress_with_ffmpeg

def compress_with_ffmpeg
  [:thumb, :original, :medium].each do |type|
    img_path = self.avtar.path(type)
    Paperclip.run("ffmpeg", " -i #{img_path} #{img_path}")
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜