Skip to content

Instantly share code, notes, and snippets.

@johnboxall
Created December 22, 2014 23:30

Revisions

  1. johnboxall renamed this gist Dec 22, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. johnboxall created this gist Dec 22, 2014.
    37 changes: 37 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # 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