Skip to content

Instantly share code, notes, and snippets.

@mkalygin
Created September 13, 2018 10:36
Show Gist options
  • Save mkalygin/94155aca01f7b22e3719f6892903c254 to your computer and use it in GitHub Desktop.
Save mkalygin/94155aca01f7b22e3719f6892903c254 to your computer and use it in GitHub Desktop.
This simple batch script compresses PNG and JPEG files with TinyPNG API https://tinypng.com. It depends on jq tool https://stedolan.github.io/jq/.
# Usage:
# ./tinypng <path> <path> ...
#
# Upload the files to shrink and then save them locally under the same name.
# Only works with JPEG and PNG. Accepts wildcard path for batch compression.
#
# Uses TinyPNG API: https://tinypng.com/developers/reference.
# Uses TINYPNG_API_KEY environment variable for TinyPNG API key.
# Depends on jq: https://stedolan.github.io/jq/
set -euo pipefail
API_KEY=$TINYPNG_API_KEY
API_URL=https://api.tinypng.com/shrink
EMOJIS=("🔥" "⭐️" "❤️" "💯" "👌" "🐝" "💩")
for file in $@ ; do
emoji=${EMOJIS[$RANDOM % ${#EMOJIS[@]} ]}
url=$(curl -sf --user "api:$API_KEY" --data-binary @$file $API_URL | jq -r '.output.url')
curl -sf -o "$file" "$url"
echo "$emoji optimized $file"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment