This is an updated version of the imgopt script written by Joel Hardi.

The included bash script imgopt uses find to recursively locate JPEG/PNG files and process them with jpegtran, jfifremove, optipng, advpng and pngout, using settings to losslessly minimize file size and strip all metadata headers.

So, you can easily optimize every image in your website (“imgopt htdocs”), just a single file (“imgopt favicon.png”) or any combination (“imgopt favicon.png images/ uploads/”)!

Any files the script is able to reduce in size, it overwrites in place. It does not write to or overwrite files that it cannot reduce in size.

The script is commented to make it easy for you to edit if you would like to customize the helper programs used, or their runtime options.

By default, all image transformations are lossless. (Stripping of metadata is, of course, not reversible.) But, there’s always the possibility of bugs, and I’m not the author of any of the helper programs used, so I strongly suggest that you not run this script on your only copy of a file! Use at your own risk.

The intended use of this script is the absolute minimization of file size to maximize performance of high-traffic websites — I recommend you use it as the last stage in a publish/build workflow of your web graphics and always keep original graphic source files somewhere.

Included is a small program, jfifremove.c, to strip the optional JFIF header segment from JPEG files. I don’t claim authorship — I found it at devshed, compared what it does to the JFIF segment format standard, fixed a few small errors and named it jfifremove. I’ve tested it thoroughly as it is used in this script — deleting the JFIF segment from files that already have had all other optional headers stripped by jpegtran. I have *not* tested it thoroughly for standalone use (i.e. with files containing thumbnails, EXIF data) so I cannot recommend you use it on its own on JPEG files in the wild. This is just FYI — I have no reason to believe that it *isn’t* OK for standalone use; I’m just telling you that I haven’t tested it that way, so you should use caution.

Fork me on GitHub