Skip to content

Instantly share code, notes, and snippets.

@vayn
Created May 12, 2011 19:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vayn/969258 to your computer and use it in GitHub Desktop.
Save vayn/969258 to your computer and use it in GitHub Desktop.
Compress PNG file with Pngcrush
#!/bin/bash
# Name of File: pngcrusher.sh
# Author: Vayn a.k.a. VT <vayn@vayn.de>
# Name: PNG Crusher
# Thanks To: Jyo <http://jyorr.com>
# License:
# Released under the MIT, BSD, and GPL Licenses.
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
E_BADDIR=65
usage() {
echo "PNG Crusher extends function of Pngcrush to compress all png files in a directory.
Usage: `basename $0` [option]... [dir]...
-h help"
}
crush() {
dir=$(readlink -f "$1")
if [[ ! -d "$dir" ]]; then
echo "Cannot open \"$dir\" as directory."
exit $E_BADDIR
else
counter=0
for filename in "$dir"/*; do
if [[ -f "$filename" && $(file "$filename" | awk '{print $2}') = PNG ]]; then
echo "Processing $filename"
pngcrush "$filename" "$filename.tmp"
mv "$filename.tmp" "filename"
((counter++))
fi
done
if [[ $counter -gt 0 ]]; then
echo "$counter png file(s) processed."
exit 0
else
echo "No png file found."
exit 1
fi
fi
}
if [[ $# -eq 0 || "$1" = "-h" ]]; then
usage;
exit 0;
else
crush "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment