I hope this question fits here. I would have asked on Stackoverflow, but there is a similar question there that was closed with the suggestion to ask at Server Fault, and Server Fault closed my question with the suggestion to ask here (which is extremly frustrating):


I need to automatically compress images (uploaded by users) so Google Page Speed Insights doesn't complain about the image size anymore. Using the Google Page Speed Mod for Apache/Nginx is not an option.

I looked at the official image compression FAQ here: https://developers.google.com/speed/docs/insights/OptimizeImages.

They suggest jpegtran and jpegoptim for jpeg images. I tried both with various different settings, but they didn't compress the images any further, and sometimes the images actually got bigger.

Then I tried Guetzli, a new jpeg encoder from Google itself: https://github.com/google/guetzli.

This did compress the images much better than before, but Page Speed Insights still complains that the images are to big.

So how can I compress (hundreds of) Jpegs automatically (on a linux machine) so Google Page Speed Insights doesn't complain anymore?

Edit: To clarify: Google Insights allows you to download optimized images, and they really are much smaller than anything I am able to produce. How can I compress the images (slight quality loss is okay) so they are as small as the images I can download from Google Insights? Since there are hundreds of images on the site using Google Insight to compress them all isn't really practical.

Are there any command line tools for linux that will compress as well as Google Insight?

(There is a protected question on stackoverflow from 2011 but none of the answers work anymore: https://stackoverflow.com/questions/5451597/how-does-googles-page-speed-lossless-image-compression-work?rq=1)

  • From my testing today, I've found that the Google Pagespeed tool actually resizes images (changing the pixel dimensions) but goes far too small to show up crisp on the website. I'm not sure if this is a bug or if it's what they expect these days. Confusing. – Simon East May 24 '17 at 2:41

Lossless VS Normal Compression

Looks like you're getting your wires mixed, Google insights refers to lossless compression, not the JPEG encoder. jpegoptim, jpegtran, jpegrescan, mozjpeg1, mozjpeg2 and so on are all lossless compression tools, not JPEG encoders such as Guetzli.

What this means is it takes an already compressed file and makes it even smaller by stripping down the things it doesn't need, such as hidden META information.

Effective you are compressing the file twice. Generally the JPEG compression in most imaging software such as Photoshop will do, you just need to compression it again using a lossless application.

Caching Issues

If you have tried jpegtran but still got Google insights nagging at you then it may be caused server-side caching which is still delivering the old version of the image, this is common with content delivery networks and reverse HTTP proxy's.

If you are using aws s3, vanish, CloudFlare or a similar service you will need to purge the cache before testing.

Lossless Compression, Different Results.

You will often find that lossless compression will vary from one image ot the next depending on the software your using, some images compress better in one tool but may not do better in the next and vice versa.

Many people will use a script or application that will run the file in multiple lossless compressions and find the best results, this is particular more useful in PNG than JPEG because the results can actually be more than 5-50kb different.

You will find with lossless compression applications, results vary depending on the image, not the application, so often you have to use an array of lossless compression and pick the best size.

Thankfully there are applications online that will help you do this without having to build your own script and run the image one by one through different compressions, if your a windows user, I recommend that you take a look at FileOptimizer.

  • Thank you for the detailed answer. Caching is not the issue, I compared the images with the uploaded images and the file size is the same. Google Insights allows you to download optimized images and they really are much smaller than anything I can produce (I will add that to my question), My question is how are they able to compress them so much better than I am? Do they compress them at a quality like 80 percent? – Fels Apr 27 '17 at 10:30
  • It says why in my answer. You're miss-understanding the difference between a Jpeg Encoder and lossless compression. You save your Jpeg 60-80% using Photoshop or any other imaging software. You then optimize that Jpeg using a Lossless compression. It is a two-step process. Google is simply using a lossless compression on your files as a demonstration. If you download FileOptimizer, it'll do it for you, simply drag all your JPEGS into it and hit optimize, Done! – Simon Hayter Apr 27 '17 at 14:39
  • No I understand. The images are uploaded by users, so I need to recompress them at 60-80% quality, that is what I wanted to know. I accepted your answer. – Fels Apr 28 '17 at 11:20

I use Gimp software plus a plugin that allows batch optimizing. The percentage of compression is the tricky part especially for batches. I would test at least one image before doing batches.

Not the answer you're looking for? Browse other questions tagged or ask your own question.