Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top