public
Last active

Image optimization with a home-rolled Smush.it

  • Download Gist
smushit.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#!/bin/bash
#
# Home-rolled Smush.it, by Alex Dunae (http://dialect.ca).
#
# 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 (http://jpegclub.org/jpegtran/), part of libjpeg,
# and pngcrush (http://pmt.sourceforge.net/pngcrush/index.html).
 
# Set to WordPress upload directory or any folder with images.
base_path=~/wptrunk.dunae.ca/wp-content/uploads/
 
# 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 '{}' \;
else
echo 'jpegtran not found';
fi
 
# 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 '{}' '{}' \;
else
echo 'pngcrush not found';
fi

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.