Image optimization using cronjobs

Update april 2015: the tool Smush It is recently put offline. More information about this on A possible alternative:


In my previous blogpost “Image optimization is important” I wrote about the importance of optimize images using the free service from Yahoo! called ‘‘. You just have to upload an image or enter the web link to the image and will provide you an optimized version. It’s a huge task if you want to optimize all the images used on your website and continuously repeat this every week. As a webmaster you want to serve a fast website including optimized images. And not every content editor (or even you) will upload an optimized image every time he/she will write a blogpost. Life would be easy if your website wil automatically optimizes the images once in a while. In this blogpost I will show you how you can use a couple optimizers installed on our servers… by hand and using a cronjob.

Optimize your JPG’s with JpegOptim

Use the shell command below to find all your JPG images and optimize them using jpegoptim. JpegOptim is a JPG optimizer used to optimize/compress jpeg files.

Flag –strip-all will strip all (Comment & Exif) markers from the output file. The option -p is added to preserve file modification times. Be aware that when you optimize progressive jpg, result will be a normal jpg. Developer Ann Robson wrote blog “Progressive jpegs: a new best practice” on the Performance Advent Calendar.

Appearing faster is being faster, and perceived speed is more important that actual speed.

Ann Robson in Performance Calendar 

Optimize your PNG’s with OptiPNG

Use the shell command below to find all your PNG images and optimize them using OptiPNG. OptiPNG is a PNG optimizer that recompresses image files to a smaller size, without losing any information.

Flag -o7 means the level of optimization. The higher the level the more compression trials. The trial chosen is what OptiPNG thinks it’s probably the most effective. The option -preserve will preserve file attributes (time stamps, file access rights, etc.) where applicable. Add the option -keep if you want to back up the original file.

OptiPNG can also optimize non-png file formats. It will create an optimized PNG version of the given file. The output file name is composed from the original filme name and the .png extension.

Combine optimization in a cron job

Cronjobs can make life more easy. They allow you to run tasks on unix automatically in the background at a regular interval. The commands mentioned in this blog can be used to optimize your images automatically.

These cronjobs will be executed once a week (Sunday) and will only optimize the images modified in the last 7 days. It’s no use to optimize images which has been optimized a week before. It’s an unnecessary use of resources. The added flags to preserve modification times proves it’s use now. When the image is optimized it will keep it’s timestamp to prevent the image will be optimized next week.

By request of our tech dudes I also added lockrun to the cronjobs. It will make your cronjob more error proof. Read more about the basics of lockrun on our knowledge base.

You should always use lockrun in cronjobs.

Erik Pluijmen

Go to to measure the results before and after. You will notice an improvement on your score regarding “Optimize images”. Have fun optimizing your website.

Scan je eigen Magento shop op veiligheidslekken

Related Post