Created
September 12, 2017 15:54
-
-
Save maqnouch/1b10b815306085e7d9027f955444a242 to your computer and use it in GitHub Desktop.
Bash Shell Script To Bulk Optimize Images With 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 | |
TINYAPIKEY="YOUR_API_KEY" | |
# Make sure source dir is supplied | |
if [ -z "$1" ] | |
then | |
echo "Missing argument. Supply the source directory containing the images to be optimized. Usage: ./tiny.sh <source_dir>" | |
exit 1 | |
fi | |
INDIR=$1 | |
# Make sure source dir exists | |
if [ ! -d "$INDIR" ]; then | |
echo "\"$INDIR\" directory not found. Supply the source directory containing the images to be optimized. Source directory should be relative to location of this script. Usage: ./tiny.sh <source_dir>" | |
exit 1 | |
fi | |
DIRNAME=${INDIR##*/} | |
OUTDIRNAME="${DIRNAME}_tiny" | |
OUTDIR="`pwd`/$OUTDIRNAME" | |
# Make sure output dir does not already exist with files. | |
# If dir exists but empty, it's ok, we proceed. | |
if find "$OUTDIRNAME" -mindepth 1 -print -quit | grep -q .; then | |
echo "Output directory ($OUTDIRNAME) already exists. Exiting without optimizing images." | |
exit 1 | |
fi | |
# Create output dir if it does not already exist | |
if [ ! -d "$OUTDIRNAME" ]; then | |
echo "Creating output dir \"$OUTDIRNAME\"..." | |
mkdir $OUTDIRNAME | |
fi | |
# Begin optimizing images | |
echo "Optimizing images..." | |
cd $INDIR | |
shopt -s nullglob | |
for file in *.png *.PNG *.jpg *.JPG *.jpeg *.JPEG | |
do | |
Cfile=`curl https://api.tinify.com/shrink --user api:$TINYAPIKEY --data-binary @"${file}" --dump-header /dev/stdout --silent | grep Location | awk '{print $2 }'` | |
Cfile=${Cfile// } | |
Cfile=`echo -n "$Cfile"| sed s/.$//` | |
curl ${Cfile} -o "${OUTDIR}/${file}" --silent | |
done | |
# Gather stats | |
echo "Gathering stats..." | |
INDIR_SIZE="$(du -h)" | |
INDIR_FILE_COUNT="$(ls | wc -l)" | |
cd $OUTDIR | |
OUTDIR_SIZE="$(du -h)" | |
OUTDIR_FILE_COUNT="$(ls | wc -l)" | |
echo -e "Finished.\r\nOriginal directory ($INDIR) has $INDIR_FILE_COUNT files; total size $INDIR_SIZE\r\nTinified directory ($OUTDIRNAME) has $OUTDIR_FILE_COUNT files; total size $OUTDIR_SIZE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment