Script to optimize JPEG & PNG images (meant to be run as a @daily Cron job)
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 | |
# Usage | |
# imageoptim.sh /path/to/images/dir | |
# | |
# See: https://gist.github.com/rungta/0cc3f68beee42008e5cf05b2aa526954 | |
# Find image files modified in the last 24 hours | |
jpgfiles=`find $1 -mtime -1 -name '*.jp*g'` | |
pngfiles=`find $1 -mtime -1 -name '*.png'` | |
# Optimize JPGs | |
# overwrite only if file size diff > than 1% | |
for i in $jpgfiles; do | |
echo "Optimising $i" | |
/usr/bin/jpegoptim -s -m85 -T1 "$i" | |
done | |
# Optimize PNGs | |
# based on http://sweetme.at/2013/09/11/how-to-maximize-png-image-compression-with-optipng/ | |
# (No overwriting check needed) | |
for i in $pngfiles; do | |
echo "Optimising $i" | |
/usr/bin/optipng -o2 -strip all "$i" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment