-
-
Save dkesberg/94a033bea111af91472eb2fa36cf7ea7 to your computer and use it in GitHub Desktop.
Script for JPEG images optimization
This file contains hidden or 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/sh | |
# Usage 1: | |
# optimize-images.sh /images/dir | |
# | |
# Usage 2: | |
# cd /images/dir | |
# optimize-images.sh | |
EXTENSIONS="jpe?g" | |
if [ -z "$1" ]; then | |
DIR="`pwd`" | |
else | |
DIR="$1" | |
fi | |
# Optimize JPEG images | |
find "$DIR" -regextype posix-egrep -regex ".*\.($EXTENSIONS)\$" -type f | xargs -I{} jpegtran -optimize -progressive -outfile "{}.optimized" "{}" | |
# Rename xxx.jpg.optimized -> xxx.jpg | |
find "$DIR" -name '*.optimized' -print0 | while read -d $'\0' file; do | |
chown $(stat -c "%U:%G" "${file%.optimized}") "$file" | |
chmod $(stat -c "%a" "${file%.optimized}") "$file" | |
mv -f "$file" "${file%.optimized}"; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment