Skip to content

Instantly share code, notes, and snippets.

@johnboxall
Created December 22, 2014 23:30
Show Gist options
  • Save johnboxall/0aa56a113d24c10a677f to your computer and use it in GitHub Desktop.
Save johnboxall/0aa56a113d24c10a677f to your computer and use it in GitHub Desktop.
TinyPNG script.
# Usage:
# ./tinypng.sh <filename> <filename> <filename>
#
# Upload the files to shrink and then save them locally under the same name.
# Only works with JPEG and PNG.
#
# Uses the TinyPNG API: https://tinypng.com/developers/reference
# Inspired by this Gist: https://gist.github.com/s4l1h/553d00b71d4ab14c17d9
#
# TODO:
# * Only allow PNG and JPEG
# * Confirm the images you want to resize.
# * Test that images always get smaller.
# * Don't fail anywhere.
# * Add to githooks to automatically crush new files.
#
# set -x
set -e
set -o pipefail
KEY=$TINYPNG_API_KEY
if [ -z $TINYPNG_API_KEY ] ; then
echo "Must set \$TINYPNG_API_KEY."
exit 1
fi
# -s = silent, no progress meter.
# -i = include HTTP headers in the response, useful to grab the Location!
# -f = fail, return non-zero if the command fails.
# -o = output file
# --data-binary = read from file
for f in $@ ; do
url=$(curl -sif --user "api:$KEY" --data-binary @$f https://api.tinypng.com/shrink | grep "Location:" | awk -F ' ' '{print $2}')
curl -sf -o "$f" "$url"
echo "Wrote $f"
done
@johnboxall
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment