Created
April 15, 2016 19:11
-
-
Save colinmollenhour/823baa257b0fdc6c38b1eb97d34a8137 to your computer and use it in GitHub Desktop.
Optimize PNG and JPEG images - incrementally
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 | |
# | |
# Optimize all jpg and png files in the cwd | |
# Run it again to optimize new files since the last run | |
# | |
# Example: | |
# echo "optimize_media.sh > /tmp/optimize.log" | sudo -u www-data bash -s | |
# | |
# Colin Mollenhour 2016 | |
user=www-data | |
dir=$PWD | |
flag=$dir/.last-optimized | |
[ $(whoami) = "$user" ] || { echo "Use 'sudo -u $user' to run this script." >&2; exit 1; } | |
type optipng 2>&1 > /dev/null || { echo "optipng is not installed." >&2; exit 1; } | |
type jpegoptim 2>&1 > /dev/null || { echo "jpegoptim is not installed." >&2; exit 1; } | |
touch ${flag}-new || { echo "Cound not touch new flag file." >&2; exit 1; } | |
date; du -sh $dir | |
opts="-type f" | |
if [ -f "$flag" ]; then | |
opts="$opts -newer $flag" | |
fi | |
find $dir $opts -iname '*.png' -not -path "*/watermark/*" \ | |
-exec optipng -o7 -preserve {} \; | |
find $dir $opts \( -iname '*\.jpg' -or -iname '*\.jpeg' \) \ | |
-exec jpegoptim --strip-all -p {} \; \ | |
-exec chmod 660 {} \; | |
date; du -sh $dir | |
mv ${flag}-new $flag |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment