2017-12-05 22:39:10 -08:00
|
|
|
#!/usr/bin/env zsh
|
2017-06-30 03:06:14 -07:00
|
|
|
|
2017-12-17 22:29:37 -08:00
|
|
|
# Optimize image files (losslessly by default)
|
2017-06-30 03:06:14 -07:00
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
# imgoptim image.jpg image.png image.gif
|
|
|
|
# imgoptim directory_of_images/
|
|
|
|
# imgoptim directory_of_images/*.png
|
|
|
|
#
|
|
|
|
# Requires:
|
|
|
|
# PNGs: optipng, pngquant (lossy)
|
|
|
|
# JPGs: jpegtran, jpegoptim (lossy)
|
|
|
|
# GIFs: gifsicle
|
2017-12-17 22:29:37 -08:00
|
|
|
#
|
|
|
|
# Packages (same name on homebrew & arch linux)
|
|
|
|
# optipng [pngquant] libjpeg-turbo [jpegoptim] gifsicle
|
2017-06-30 03:06:14 -07:00
|
|
|
|
|
|
|
set -e
|
2017-12-05 22:39:10 -08:00
|
|
|
unset CDPATH
|
2017-06-30 03:06:14 -07:00
|
|
|
|
2017-12-17 22:29:37 -08:00
|
|
|
_usage() {
|
2017-12-05 22:39:10 -08:00
|
|
|
cat <<EOL
|
|
|
|
Usage: ${0:A:t} [-lh] IMAGE [IMAGE2 [IMAGE3 [...]]]
|
2017-06-30 03:06:14 -07:00
|
|
|
|
2017-12-05 22:39:10 -08:00
|
|
|
-l enable lossy compression
|
|
|
|
-h this help
|
2017-06-30 03:06:14 -07:00
|
|
|
EOL
|
|
|
|
}
|
|
|
|
|
|
|
|
_filesize() {
|
2017-12-05 22:39:10 -08:00
|
|
|
case $OSTYPE in
|
|
|
|
darwin*) stat -c%s "$1" ;;
|
|
|
|
*) stat --printf="%s" "$1" ;;
|
|
|
|
esac
|
2017-06-30 03:06:14 -07:00
|
|
|
}
|
|
|
|
|
2017-12-17 22:29:37 -08:00
|
|
|
#
|
|
|
|
while getopts hl opt; do
|
|
|
|
case $opt in
|
|
|
|
h) _usage; exit;;
|
|
|
|
l) lossy=1;;
|
|
|
|
:) >&2 echo "$OPTARG requires an argument"; _usage; exit 1;;
|
|
|
|
*) >&2 echo "Not a valid arg: $opt"; _usage; exit 1;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
|
|
|
|
cmds=( optipng jpegtran gifsicle )
|
|
|
|
[[ $lossy ]] && cmds=( $cmds pngquant jpegoptim )
|
|
|
|
for cmd in "optipng jpegtran gifsicle"; do
|
|
|
|
if ! command -v $cmd >/dev/null; then
|
|
|
|
>&2 echo "$cmd isn't installed"
|
|
|
|
error=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [[ $error ]]; then
|
|
|
|
>&2 echo "There were errors, aborting"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2017-06-30 03:06:14 -07:00
|
|
|
for file in $@; do
|
2017-12-05 22:39:10 -08:00
|
|
|
if [[ -d $file ]]; then
|
|
|
|
imgoptim $file/*
|
|
|
|
elif [[ -f $file ]]; then
|
|
|
|
pre_size=$(_filesize "$file")
|
|
|
|
case ${file##*.} in
|
|
|
|
png)
|
2017-12-17 22:29:37 -08:00
|
|
|
[[ $lossy ]] && pngquant $file
|
2017-12-05 22:39:10 -08:00
|
|
|
optipng -nc -nb -o7 $file
|
|
|
|
;;
|
|
|
|
gif)
|
|
|
|
gifsicle --batch --optimize=3 "$file"
|
|
|
|
;;
|
|
|
|
jpg|jpeg)
|
2017-12-17 22:29:37 -08:00
|
|
|
[[ $lossy ]] && jpegoptim --max=90 "$file"
|
2017-12-05 22:39:10 -08:00
|
|
|
jpegtran -copy none -optimize -progressive -outfile "$file" "$file"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
printf "Unrecognized file '$file': ignored"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
post_size=$(_filesize "$file")
|
|
|
|
perc=$(echo "((${pre_size} - ${post_size}) / ${pre_size}) * 100" | bc -l)
|
|
|
|
printf "* %s: %d => %d (%.2f%% reduction)\n" "$file" "${pre_size}" "${post_size}" "$perc"
|
|
|
|
fi
|
2017-06-30 03:06:14 -07:00
|
|
|
done
|