Compressing Images with a Copy of Rails Compared to What the Speed ​​Page Produces

I created a paperclip in rails and everything works hunky-dory (actually I needed to do this on Google ... :).

I noticed, however, that page speed tells me that I could losslessly compress my thumbnails and large images (those produced by the clip). Is there a way that I can contribute to my model that does this? I noticed that mod_deflate does not compress images (I use Firefox).

+6
source share
3 answers

You can add compression to paper clip processing using paperclip-compression pearls .

In your gemfile:

gem "paperclip-compression", "~> 0.1.1" 

(of course, run the package installation)

In your model:

 has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :processors => [:thumbnail, :compression] 

"jpegtran works by rearranging compressed data (DCT coefficients) without fully decoding the image. Therefore, its lossless transformations "

Note. If you use a hero, you will need jpegtran, and optipng binaries will be added to your application. Here is a good article on running binary files on a hero .

+10
source

You should do the testing yourself at different levels of JPEG compression, but I noticed that I can improve ImageMagicks image quality to 75 and still do not see a noticeable difference - with about 30-40% file saving.

My model looks like this:

  has_attached_file :photo, :styles => { :"185x138" => { :geometry => "185x138>" } }, :convert_options => { :all => "-auto-orient", :"185x138" => "-quality 75", 

-quality 75 for ImageMagick. If you use a different processor, you will need to configure accordingly.

+1
source

How 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
source

Source: https://habr.com/ru/post/891879/


All Articles