Skip to content

Instantly share code, notes, and snippets.

@volker48
Forked from ryansully/optimize.sh
Last active December 4, 2015 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save volker48/ea82c3b03bca2dc3d429 to your computer and use it in GitHub Desktop.
Save volker48/ea82c3b03bca2dc3d429 to your computer and use it in GitHub Desktop.
Optimized image optimization script (pngcrush & jpegtran)
#!/bin/sh
# script for optimizing images in a directory (recursive)
# pngcrush & jpegtran settings from:
# http://developer.yahoo.com/performance/rules.html#opt_images
# pngcrush
for file in `find -E . -regex '.*\.(jpg|png)'`; do
if [ ${file: -4} == ".png" ]
then
echo "crushing $file ..."
pngcrush -rem alla -reduce -brute "$file" temp.png
# preserve original on error
if [ $? = 0 ]; then
mv -f temp.png $file
else
rm temp.png
fi
else
echo "crushing $file ..."
jpegtran -copy none -optimize -progressive "$file" > temp.jpg
# preserve original on error
if [ $? = 0 ]; then
mv -f temp.jpg $file
else
rm temp.jpg
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment