Skip to content

Instantly share code, notes, and snippets.

@gustavomdsantos
Last active August 17, 2021 14:33
Show Gist options
  • Save gustavomdsantos/a55b06f8d3e9212a74b9d4271a3be8a1 to your computer and use it in GitHub Desktop.
Save gustavomdsantos/a55b06f8d3e9212a74b9d4271a3be8a1 to your computer and use it in GitHub Desktop.
TinyPNG for Bash - compress PNG files in command-line interface.

TinyPNG for Bash

Simple script to compress PNG files faster than in web browser. This works in any operating system with Bash installed.

  1. Get your API Key in tinypng.com/developers.
  2. Edit tinypng.sh file, overwriting pJNqqHfKLj8Cs7DMFjXrx8rvZ8QrYvVD with your API Key.
  3. If you're:
    1. In a UNIX operating system:
      • Run chmod to all .sh files;
      • Run INSTALL.sh;
    2. In Windows (with MINGW or "Git Bash"):
      • Open MINGW in Administrator Mode;
      • Run INSTALL.sh.
  4. Run tinypng yourImage.png.
#!/usr/bin/env bash
echo
if ! uname | grep -q MINGW64
then
set -x
sudo cp tinypng.sh /usr/bin/tinypng -v
set +x
echo
echo "Installation successful!"
else
set -x
cp tinypng.sh /usr/bin/tinypng -v
if [ $? -ne 0 ]
then
set +x
echo
echo "Try to run this script with the Terminal in Administrator Mode!"
else
set +x
echo
echo "Installation successful!"
fi
fi
#!/usr/bin/env bash
set -e
clear
echo "--------------------"
echo "| TINYPNG FOR BASH |"
echo "--------------------"
echo
echo "[1/2] Uploading file to TinyPNG API..."
echo
apiResponse=$(curl https://api.tinify.com/shrink \
--user api:pJNqqHfKLj8Cs7DMFjXrx8rvZ8QrYvVD --data-binary @"$1")
downloadURI=$(echo "${apiResponse}" | cut -d ':' -f 12 | sed 's/"}}//g')
echo
echo "[2/2] Downloading compressed PNG from temporary download URL:"
downloadURL="https:${downloadURI}"
echo "${downloadURL}"
echo
curl -o "${1}" "${downloadURL}"
set +e
#!/usr/bin/env bash
echo
if ! uname | grep -q MINGW64
then
set -x
sudo rm /usr/bin/tinypng -v
set +x
echo
echo "Uninstalation successful!"
else
set -x
rm /usr/bin/tinypng -v
if [ $? -ne 0 ]
then
set +x
echo
echo "Try to run this script with the Terminal in Administrator Mode!"
else
set +x
echo
echo "Uninstalation successful!"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment