Skip to content

Instantly share code, notes, and snippets.

@rungta
Last active June 19, 2017 14:33
Show Gist options
  • Save rungta/0cc3f68beee42008e5cf05b2aa526954 to your computer and use it in GitHub Desktop.
Save rungta/0cc3f68beee42008e5cf05b2aa526954 to your computer and use it in GitHub Desktop.
Script to optimize JPEG & PNG images (meant to be run as a @daily Cron job)
#!/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