Skip to content

Instantly share code, notes, and snippets.

@iccir
Last active July 6, 2017 03:12
Show Gist options
  • Save iccir/85549cbc426062ef07791a590d85e42c to your computer and use it in GitHub Desktop.
Save iccir/85549cbc426062ef07791a590d85e42c to your computer and use it in GitHub Desktop.
cpng
#!/bin/sh
IFS=$'\n'
FORCE=0
compress_png () {
more_args=""
# Note: I use an "og_" prefix on a file to indicate "opaque and grayscale",
# "g_" for grayscale, and "o_" for opaque.
if [[ $1 = og_* ]]; then
more_args="-c 0"
elif [[ $1 = g_* ]]; then
more_args="-c 4"
elif [[ $1 = o_* ]]; then
more_args="-c 2"
fi
# Add a "[[cpng]]" text tag to the file so we don't spend time
pngcrush $more_args -rem alla -text b "[[cpng]]" "[[cpng]]" -e ".png_tmp" "$1"
optipng -o7 "$f"_tmp
mv "$f"_tmp "$f"
}
for f in $*
do
if [ "$f" == "-f" ]; then
FORCE=1
continue
fi
pngcheck -q -t "$f" | grep -q "[[cpng]]"
if [ $? -ne 0 ] || [ $FORCE -eq 1 ]; then
compress_png "$f"
else
echo "$f" "already compressed"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment