Last active
August 27, 2019 13:55
-
-
Save M-rcus/32e4cae3c19cb243166fea9ffcaef87c to your computer and use it in GitHub Desktop.
Script for converting file(s) using TinyPNG API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A TinyPNG API script that at least works... somewhat. | |
# This isn't a pretty script, but it works. Don't h8 me. | |
# This script is meant for singular files. But you can use convert all files in a folder using a `for` loop. | |
# Example: `for image in *.jpg; do ./tinypng_convert.sh $image; done` | |
# What this script does: | |
# - Takes one file as input | |
# - Submits to TinyPNG API. | |
# - Downloads file into a new filename based on a few parameters (see description of `FILENAME_ADDON`) | |
# - Returns a few stats. | |
# Requirements: curl, jq | |
# Easy to get on Debian/Ubuntu: `sudo apt install curl jq` | |
# You can probably get it on other distros, but I don't use those so ¯\_(ツ)_/¯ | |
# curl: https://curl.haxx.se/ | |
# jq: https://stedolan.github.io/jq/ | |
# Get TinyPNG API key: https://tinypng.com/ | |
# 500 free conversions per month. | |
TINYPNG_API_KEY="" | |
# Filename addon is placed between the original filename and the new extension based on TinyPNG's API. | |
# Basically with the following parameters: | |
# Original filename: "TinyPNG_Test_File.png" | |
# Filetype returned from TinyPNG API: "image/jpeg", which is translated to: "jpg" | |
# FILENAME_ADDON: "TinyPNG" | |
# ... the new filename would be turned into: "TinyPNG_Test_File.TinyPNG.jpg" | |
# | |
# ** If FILENAME_ADDON is empty, it would be: "TinyPNG_Test_File.jpg" | |
# ** If filetypes DON'T CHANGE (which is 99% of the cases), this will REPLACE the original file. | |
# ** Consider this your warning. | |
# | |
# Hope that makes sense | |
FILENAME_ADDON="t" | |
if [[ -z "$1" ]]; then | |
echo "Missing parameter: file" | |
exit 1 | |
fi | |
FILE="$@" | |
echo "Submitting $FILE to TinyPNG API." | |
RESPONSE=$(curl --silent --data-binary "@$FILE" --user "api:$TINYPNG_API_KEY" https://api.tinify.com/shrink) | |
# Get the download URL of the tinified file. | |
DOWNLOAD_URL=$(echo $RESPONSE | jq -r '.output.url') | |
echo "Parsing response from TinyPNG" | |
# Get filesizes in human-readable formats. | |
OLD_SIZE=$(echo $RESPONSE | jq '.input.size' | numfmt --to=si --suffix=B) | |
NEW_SIZE=$(echo $RESPONSE | jq '.output.size' | numfmt --to=si --suffix=B) | |
# Correct filetype | |
TYPE=$(echo $RESPONSE | jq -r '.output.type' | sed 's/image\///g') | |
if [[ $TYPE == "jpeg" ]]; then | |
TYPE="jpg" | |
fi | |
# Create filename based on filetype. | |
NEW_FILENAME="$(echo $FILE | sed 's/\.[^.]*$//')" | |
# Only apply filename addon if it's not empty. | |
if [[ $FILENAME_ADDON == "" ]]; then | |
NEW_FILENAME="$NEW_FILENAME.$TYPE" | |
else | |
NEW_FILENAME="$NEW_FILENAME.$FILENAME_ADDON.$TYPE" | |
fi | |
echo "Downloading new file from TinyPNG" | |
curl --silent -o "$NEW_FILENAME" "$DOWNLOAD_URL" | |
echo "Converted $FILE: $OLD_SIZE => $NEW_SIZE" | |
echo "New filename: $NEW_FILENAME" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment