Batch Optimizing Images

I already wrote about optimizing images for your website. What I didn't tell at that time is that, since I ran tools under Windows, this meant a lot of download/upload shenanigans. Yes, it was scriptable but annoying nonetheless. What I needed was a way to run both OptiPNG and jpegoptim automatically on the web host itself.

These pages are hosted by DreamHost which currently runs on Debian (Wheezy) and its linux environment is rather rich. Despite, neither of my preferred tools was installed and, since this was not my machine, just installing a package was also not really possible.

However, one thing I could do is to actually build them from sources. With jpegoptim this is easy as it uses GitHub for source. With OptiPNG it gets a bit more involved but nothing too far from just a basic download and compile:

$ mkdir -p ~/bin

$ cd ~/bin
$ git clone
$ cd jpegoptim/
$ ./configure
$ make clean
$ make

$ cd ~/bin
$ wget -O /tmp/optipng.tgz
$ mkdir optipng
$ cd optipng
$ tar -xzvf /tmp/optipng.tgz --strip-components 1
$ rm /tmp/optipng.tgz
$ ./configure
$ make clean
$ make

With both tools compiles, we can finally go over all the images and get them into shape:

$ find ~/www -iname '*.jpg' -print0 | xargs -0 ~/bin/jpegoptim/jpegoptim --preserve --strip-all --totals
$ find ~/www -iname '*.png' -print0 | xargs -0 ~/bin/optipng/src/optipng/optipng -o7 -preserve

For hands off approach, I also scheduled them to run every week.

[2017-12-27: If you get error Cannot find libjpeg or you have too old version, install libjpeg-turbo-devel package.]

Leave a Reply

Your email address will not be published. Required fields are marked *