Skip to content

Instantly share code, notes, and snippets.

@sergeylukin
Last active July 3, 2019 07:15
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save sergeylukin/6510605 to your computer and use it in GitHub Desktop.
Save sergeylukin/6510605 to your computer and use it in GitHub Desktop.
Shell script for JPGs and PNGs optimization
#!/usr/bin/env sh
#
# This script automates images optimization
# is mainly used by a developer who manually
# can execute it when adding new images to the
# project
#
# Download and compile following binaries:
#
# - [jpegtran](http://www.ijg.org/)
# - [pngcrush](http://pmt.sourceforge.net/pngcrush)
# - [OptiPNG](http://optipng.sourceforge.net/)
# - [pngout](http://advsys.net/ken/utils.htm)
# - [advpng](http://advancemame.sourceforge.net/comp-readme.html)
#
SCRIPTPATH=$(readlink -f "$0")
# Get directory path of this script
SCRIPTDIR=$(dirname "$SCRIPTPATH")
# Set path to directory with images
IMAGESDIR="$SCRIPTDIR/app/assets/images"
##################
# Optimize JPEGS #
##################
if command -v jpegtran > /dev/null 2>&1
then
echo "Optimizing JPEG images"
find $IMAGESDIR \( -name "*.jpg" -o -name "*.jpeg" \) -exec jpegtran -copy none -optimize -outfile '{}' '{}' \;
else
echo "Hmm, you're missing JPEGTRAN. I recommend you install it for best result"
fi
#################
# Optimize PNGs #
#################
if command -v pngcrush > /dev/null 2>&1
then
echo "Optimizing PNG images using PNGCRUSH"
for png in $(find $IMAGESDIR -name "*.png")
do
pngcrush -reduce -brute -q $png $png-optimized && mv $png-optimized $png
done
else
echo "Hmm, you're missing PNGCRUSH. I recommend you install it for best result"
fi
if command -v optipng > /dev/null 2>&1
then
echo "Optimizing PNG images using OptiPNG"
find $IMAGESDIR -name "*.png" -exec optipng -o7 -q '{}' \;
else
echo "Hmm, you're missing OptiPNG. I recommend you install it for best result"
fi
if command -v pngout > /dev/null 2>&1
then
echo "Optimizing PNG images using PNGOUT"
for png in $(find $IMAGESDIR -name "*.png")
do
pngout $png -q -y -k0 -s0
done
else
echo "Hmm, you're missing PNGOUT. I recommend you install it for best result"
fi
if command -v advpng > /dev/null 2>&1
then
echo "Optimizing PNG images using ADVPNG"
for png in $(find $IMAGESDIR -name "*.png")
do
advpng -z -4 $png
done
else
echo "Hmm, you're missing ADVPNG. I recommend you install it for best result"
fi
@iqbalhsn20
Copy link

I want to run this script in my humhub project. but I don't have any knowledge how to run script on server. please guide me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment