Skip to content

Instantly share code, notes, and snippets.

@shaneriley
Forked from ryansully/optimize.sh
Created April 7, 2016 14:28
Show Gist options
  • Save shaneriley/74e728ce70d36912b42c4e4f55ba6574 to your computer and use it in GitHub Desktop.
Save shaneriley/74e728ce70d36912b42c4e4f55ba6574 to your computer and use it in GitHub Desktop.
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 png in `find $1 -iname "*.png"`; do
echo "crushing $png ..."
pngcrush -rem alla -reduce -brute "$png" temp.png
# preserve original on error
if [ $? = 0 ]; then
mv -f temp.png $png
else
rm temp.png
fi
done
# jpegtran
for jpg in `find $1 -iname "*.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