Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Image optimization with a home-rolled
# Home-rolled, by Alex Dunae (
# N.B. This script works for me; it may not work for you.
# Since it overwrites your images with the optimized version, you should
# backup your files before running this script and do a trial run before
# getting too excited. This is your disclaimer.
# Uses jpegtran (, part of libjpeg,
# and pngcrush (
# Set to WordPress upload directory or any folder with images.
# Find files with a JPG extension recursively and process with jpegtran by
# - stripping comments
# - optimize image table
# - making the JPEG progressive
if type -P jpegtran &>/dev/null; then
echo 'Running jpegtran';
find $base_path -iname "*.jpg" -type f -exec jpegtran -outfile '{}' -copy none -optimize -progressive '{}' \;
echo 'jpegtran not found';
# Find files with a PNG extension recursively and process with pngcrush
if type -P pngcrush &>/dev/null; then
echo 'Running pngcrush';
find $base_path -iname "*.png" -type f -exec pngcrush -reduce -brute -nofilecheck '{}' '{}' \;
echo 'pngcrush not found';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment