Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the Rails asset pipeline Gzip images

How can I get the Rails asset pipeline to Gzip compress images? It compresses css and js files but not images.

EDIT

Rewritten question. Initially this was about subfolders but it seems Rails isn't compressing any images.

like image 459
Cristiano Betta Avatar asked Jan 04 '14 04:01

Cristiano Betta


People also ask

How do you Precompile rails assets?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

How does Rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.

What is assets Precompile?

rails assets:precompile is the task that does the compilation (concatenation, minification, and preprocessing). When the task is run, Rails first looks at the files in the config.assets.precompile array. By default, this array includes application.js and application.css .


2 Answers

From section 4.1.2 in the canonical Rails Guides:

When files are precompiled, Sprockets also creates a gzipped (.gz) version of your assets.

To precompile your assets, use the bundled rake task:

# from command line
RAILS_ENV=production bundle exec rake assets:precompile

UPDATE:

After some research into the subject, I've allegorically found that, while Sprockets compresses JS and CSS assets, it does not compress images. Then I came across this gem: sprockets-image_compressor

I haven't implemented it myself, but it claims to provide lossless compression of image assets using pngcrush and jpegoptim. Interestingly, the docs state the following:

If the environment doesn't have pngcrush and/or jpegoptim installed, the gem will fall back on binaries packaged with the gem.

Again, I haven't used this myself, but if it does what it claims, it might be exactly what you're looking for.

like image 198
zeantsoi Avatar answered Oct 20 '22 14:10

zeantsoi


It doesn't compress any images because images are already compressed (like jpeg). So it is not needed for traffic saving purposes.

Which means you cannot do it with any of existing settings.

like image 36
Mikhail Chuprynski Avatar answered Oct 20 '22 14:10

Mikhail Chuprynski