Skip to content

Instantly share code, notes, and snippets.

@brendannee
Forked from ryansully/optimize.sh
Created September 23, 2013 10:04
Show Gist options
  • Save brendannee/6668593 to your computer and use it in GitHub Desktop.
Save brendannee/6668593 to your computer and use it in GitHub Desktop.
#!/bin/sh
# script for optimizing images in a directory (recursive)
# pngcrush & jpegtran settings from:
# http://developer.yahoo.com/performance/rules.html#opt_images
# pngcrush
find . -iname "*.png" | while read png
do
echo "crushing $png ..."
pngcrush -rem alla -reduce "$png" temp.png
# preserve original on error
if [ $? = 0 ]; then
mv -f temp.png "$png"
else
rm temp.png
fi
done
# jpegtran
find . -iname "*.jpg" | while read jpg
do
echo "crushing $jpg ..."
jpegtran -copy none -optimize -perfect "$jpg" > temp.jpg
# preserve original on error
if [ $? = 0 ]; then
mv -f temp.jpg "$jpg"
else
rm temp.jpg
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment