Image optimization using cronjobs

I’ve been reading several articles like this one about running optipng and jpegoptim using cronjobs to automatically optimize images in /var/www/.

This seems more efficient than using a WordPress plugin to optimize images.

Is anyone else doing this? What exact cron jobs or scripts are you using?

I do this.

IMHO it is one of the best possible practices about image optimization.

Nice! Would you mind sharing your script?

It is the same you found in the link above.

Basically:

#!/bin/bash

cd /var/www
find . -name *.jp* | xargs jpegoptim --strip-all -m76
find . -iname '*.png' -print0 | xargs -0 optipng -o7 -preserve
```

It has room for a lot of improvement, and I plan, someday, to add database support in order to avoid trying to optimize the same images over and over (in case of `optipng` it is quite slow, if you have a high number of images to process.
1 Like