Skip to content

Instantly share code, notes, and snippets.

@r4vi
Forked from ryansully/optimize.sh
Last active December 15, 2015 02:29
Show Gist options
  • Save r4vi/5188004 to your computer and use it in GitHub Desktop.
Save r4vi/5188004 to your computer and use it in GitHub Desktop.
handles files with spaces in the name and keeps EXIF in jpeg
#!/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 $1 -iname "*.png" | while read png
do
echo "crushing $png ..."
optipng "$png" --out temp.png --quiet
# preserve original on error
if [ $? = 0 ]; then
mv -f temp.png $png
else
rm temp.png
fi
done
# jpegtran
find $1 -iname "*.jpg" | while read jpg
echo "crushing $jpg ..."
jpegtran -copy all -optimize -progressive -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