Skip to content

Instantly share code, notes, and snippets.

@mkalygin

mkalygin/tinypng

Created Sep 13, 2018
Embed
What would you like to do?
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