Skip to content

Instantly share code, notes, and snippets.

@swarminglogic
Last active August 29, 2015 14:06
Show Gist options
  • Save swarminglogic/cb8f3c7fe12d9fbece84 to your computer and use it in GitHub Desktop.
Save swarminglogic/cb8f3c7fe12d9fbece84 to your computer and use it in GitHub Desktop.
pngopt: A wrapper for pngcrush, that makes it easier to use for overriding images with its optimized output (e.g ./pngopt filea.png fileb.png filec.png). If a file is optimized it outputs the filename with percentage reduction.
#!/bin/bash
# pngopt: A wrapper for pngcrush, that makes it easier to use for overriding
# images with its optimized output.
#
# use: pngopt FILE1 [FILE2] [FILE3] ...
#
# If a file is optimized it outputs the filename with percentage reduction.
#
#
# Example use:
# To replace filea.png and fileb.png with its optimized counterpart:
# $ pngopt filea.png fileb.png
#
while test $# -gt 0; do
if [ ! -f "$1" ] ; then
echo "Error: Bad input file: $1" >&2
shift
continue
fi
tmpfile=$(tempfile --suffix ".png")
pngcrush "$1" $tmpfile > /dev/null
filesizes=$(ls -l "$1" $tmpfile | awk '{print $5}' | xargs echo)
if <<<$filesizes awk '{exit !($1 > $2)}' ; then
echo -n "$1 "
<<<$filesizes awk '{printf "(%.2f", (100 * ($1/$2 - 1)) ; print " %)"}'
cp $tmpfile "$1"
rm $tmpfile
fi
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment