#!/bin/sh ## imgult, the image-ultimator v4.0.02--BETA by ryanpcmcquen ## thanks to B. Watson for lots of help :^) # Ryan P.C. McQuen | Everett, WA | ryan.q@linux.com # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version, with the following exception: # the text of the GPL license may be omitted. # This program is distributed in the hope that it will be useful, but # without any warranty; without even the implied warranty of # merchantability or fitness for a particular purpose. Compiling, # interpreting, executing or merely reading the text of the program # may result in lapses of consciousness and/or very being, up to and # including the end of all existence and the Universe as we know it. # See the GNU General Public License for more details. # You may have received a copy of the GNU General Public License # along with this program (most likely, a file named COPYING). If # not, see . ## don't output file size changes by default, because du is slow ## override with SPACE_SAVING_CALC=on SPACE_SAVING_CALC=${SPACE_SAVING_CALC:-off} ## prepend imgult on the command line with ## BACKUPIMAGES=true (to back everything up) ## EXIFREMOVE=n (to keep EXIF data) BACKUPIMAGES=${BACKUPIMAGES:-false} EXIFREMOVE=${EXIFREMOVE:-true} ## create a log of files IMGULT_FILES_LIST=${IMGULT_FILES_LIST:-imgult-files.txt} IMGULT_TEMP_FILES_LIST=${IMGULT_TEMP_FILES_LIST:-imgult-temp-files.txt} ## log processed files so they can be skipped on subsequent runs IMGULT_PROCESSED_FILES_LIST=${IMGULT_PROCESSED_FILES_LIST:-imgult-processed-files.txt} ## file extensions are easily added now, ## thanks to B. Watson JPGEXTENSIONS="jpg jpeg jpe jfif jif jfi thm" PNGEXTENSIONS="png apng mng" GIFEXTENSIONS="gif" SVGEXTENSIONS="svg svgz" if [ -z "$JPEGTRAN" ]; then if [ `uname` = "Darwin" ]; then for DIR in /usr/local/Cellar/mozjpeg/*/bin do PATH="$DIR:$PATH" done fi for DIR in /opt/*/bin do PATH="$DIR:$PATH" done JPEGTRAN="$(which jpegtran)" fi if [ "${SPACE_SAVING_CALC}" = "on" ]; then ## find out how big the directory/files are ## so we can relish in the savings later du -hs "$@" > preImgultSize fi ## bsd find needs a path to work, gnu find does not; ## so we set it here if positional parameters are ## equal to 0 ## ## i was originally checking if "$@" was empty, ## but it turns out that is a nightmare if regex ## paths are fed to imgult if [ $# -eq 0 ]; then set -- . fi ## below we have leaning toothpick syndrome ## ... ## notice how the output is filtered with sed, in case ## the user feeds a directory using tab completion, ## leading to a trailing slash, and ergo, double slashes ## (most systems are fine with double slashes, ## but i avoid assumptions if possible) find "$@" -type f -a \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.jpe' \ -o -iname '*.jfif' -o -iname '*.jif' -o -iname '*.jfi' -o -iname '*.thm' \ -o -iname '*.png' -o -iname '*.apng' -o -iname '*.mng' -o -iname '*.gif' \ -o -iname '*.svg' -o -iname '*.svgz' \) \ | sed 's@//@/@g' > ${IMGULT_FILES_LIST} ## ignore any files that were already processed if [ -e ${IMGULT_PROCESSED_FILES_LIST} ]; then echo "** ... IMGATCH SERVICE INITIATED ... **" comm -13 ${IMGULT_PROCESSED_FILES_LIST} ${IMGULT_FILES_LIST} > ${IMGULT_TEMP_FILES_LIST} mv ${IMGULT_TEMP_FILES_LIST} ${IMGULT_FILES_LIST} fi ## back it up! if [ "$BACKUPIMAGES" = true ]; then rsync -avz --files-from=${IMGULT_FILES_LIST} $PWD imgult-backup-files/ fi if [ "$EXIFREMOVE" = "true" ]; then if [ "$(which exiv2)" ]; then ## clear out that exif! cat ${IMGULT_FILES_LIST} | \ while read IMGULT_FILE; do exec nice -n15 exiv2 -v rm "$IMGULT_FILE" & done else echo echo "You need to install exiv2 for exif support." echo fi fi if [ "$JPEGTRAN" ] && [ "$(which jpegoptim)" ]; then ## standard options with verbosity egrep -i "($(echo $JPGEXTENSIONS | tr ' ' '|')\$)" ${IMGULT_FILES_LIST} | \ while read IMGULT_FILE; do exec nice -n16 $JPEGTRAN -verbose -outfile "$IMGULT_FILE" "$IMGULT_FILE" \ && nice -n16 jpegoptim -v "$IMGULT_FILE" & echo "$IMGULT_FILE" >> "${IMGULT_PROCESSED_FILES_LIST}" & done else echo echo "You need to install jpegtran (mozjpeg) and jpegoptim for jpg support." echo fi if [ "$(which pngquant)" ] && [ "$(which optipng)" ]; then ## standard options with verbosity egrep -i "($(echo $PNGEXTENSIONS | tr ' ' '|')\$)" ${IMGULT_FILES_LIST} | \ while read IMGULT_FILE; do exec nice -n17 pngquant -f -v --skip-if-larger -o "$IMGULT_FILE" -- "$IMGULT_FILE" \ && nice -n17 optipng -v "$IMGULT_FILE" & echo "$IMGULT_FILE" >> "${IMGULT_PROCESSED_FILES_LIST}" & done else echo echo "You need to install pngquant and optipng for png support." echo fi if [ "$(which gifsicle)" ]; then ## -b keeps the filename, -O3 uses highest optimization level ## it is an 'O' as in Oxford, not a zero (0) egrep -i "($(echo $GIFEXTENSIONS | tr ' ' '|')\$)" ${IMGULT_FILES_LIST} | \ while read IMGULT_FILE; do exec nice -n18 gifsicle -V -b -O3 "$IMGULT_FILE" & echo "$IMGULT_FILE" >> "${IMGULT_PROCESSED_FILES_LIST}" & done else echo echo "You need to install gifsicle for gif support." echo fi if [ "$(which svgo)" ]; then egrep -i "($(echo $SVGEXTENSIONS | tr ' ' '|')\$)" ${IMGULT_FILES_LIST} | \ while read IMGULT_FILE; do exec nice -n19 svgo "$IMGULT_FILE" & echo "$IMGULT_FILE" >> "${IMGULT_PROCESSED_FILES_LIST}" & done else echo echo "You need to install svgo for svg support." echo fi ## let everything complete while [ -e ${IMGULT_FILES_LIST} ]; do if [ "`pgrep egrep`" ] \ || [ "`pgrep exiv2`" ] \ || [ "`pgrep jpegtran`" ] || [ "`pgrep jpegoptim`" ] \ || [ "`pgrep pngquant`" ] || [ "`pgrep optipng`" ] \ || [ "`pgrep gifsicle`" ] || [ "`pgrep svgo`" ] \ || [ "`lsof -- ${IMGULT_FILES_LIST}`" ]; then wait echo "... TUNING FLUX CAPACITOR ..." else if [ "${SPACE_SAVING_CALC}" = "on" ]; then ## calculate the new size here, so fast comps don't finish early du -hs "$@" > postImgultSize fi echo ## thanks to patorjk.com/software/taag/ echo "****************************************************************************** " echo " ___ ___ ___ ___ ___ " echo " ___ /\__\ /\ \ /\__\ /\__\ /\ \ " echo " /\ \ /::| | /::\ \ /:/ / /:/ / \:\ \ " echo " \:\ \ /:|:| | /:/\:\ \ /:/ / /:/ / \:\ \ " echo " /::\__\ /:/|:|__|__ /:/ \:\ \ /:/ / ___ /:/ / /::\ \ " echo " __/:/\/__/ /:/ |::::\__\ /:/__/_\:\__\ /:/__/ /\__\ /:/__/ /:/\:\__\ " echo "/\/:/ / \/__/--/:/ / \:\ /\ \/__/ \:\ \ /:/ / \:\ \ /:/ \/__/ " echo "\::/__/ /:/ / \:\ \:\__\ \:\ /:/ / \:\ \ /:/ / " echo " \:\__\ /:/ / \:\/:/ / \:\/:/ / \:\ \ \/__/ " echo " \/__/ /:/ / \::/ / \::/ / \:\__\ " echo " \/__/ \/__/ \/__/ \/__/ " echo echo "****************************************************************************** " echo if [ "${SPACE_SAVING_CALC}" = "on" ]; then echo "* ... PRE-IMGULT TIME WARP ... * " echo "* Execute pre-imgulted capacity outputting: * " cat preImgultSize echo echo "* Execute imgulted capacity outputting: * " ## find the patented imgult savings! cat postImgultSize echo echo "* Execute parametric cleaning sequence: * " rm -v preImgultSize postImgultSize ${IMGULT_FILES_LIST} else echo "* Execute parametric cleaning sequence: * " rm -v ${IMGULT_FILES_LIST} fi echo echo "* The imgult has completed. Take care. * " echo "****************************************************************************** " echo fi done