#!/bin/sh # # Requirements : pngcrush, pngnq # # pngnq quantizes a 32-bit RGBA PNG image to an 8 bit RGBA palette PNG using the neuquant algorithm. # Image optimization based on http://developer.yahoo.com/performance/rules.html#opt_images # # Usage is recursive and takes as argument a folder or file" # ##################################################################### # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # Version 2, December 2004 # # Copyright (C) 2009 curlybracket.net # Everyone is permitted to copy and distribute verbatim or modified # copies of this license document, and changing it is allowed as long # as the name is changed. # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION # 0. You just DO WHAT THE FUCK YOU WANT TO. ##################################################################### # define a logfile # LOGFILE=$HOME/png-alpha-and-optimize.log LOGFILE=./png-alpha-and-optimize.log if [ ! -f '/usr/bin/pngcrush' ] ; then echo "pngcrush is not installed." exit fi if [ ! -f '/usr/bin/pngnq' ] ; then echo "pngnq is not installed." exit fi # Put all the PNG files into an array PNGFILES=`find $1 -name "*.png"` echo "=== STARTING OPTIMIZATION ===" # we want a clean log every time we start the script echo `date` > $LOGFILE # if you want the same log for all executions of this script #echo `date` >> $LOGFILE for i in ${PNGFILES}; do echo "=== $i ===" # temporary filenames : could've been easier, but whatever. tmp_crush="$i.tmp" tmp_alpha="$i.tmp_alpha" # =========== convert to PNG alpha =============== # this operation will overwrite the existing images ! # -s : sample ; 1 samples every pixel, 3 gives good results and is default # -n : colors pngnq -s 1 -n 256 -e '.png.tmp_alpha' -f $i # =========== optimize PNG ======================== pngcrush -q -rem alla -reduce -brute $tmp_alpha $tmp_crush # =========== testing filesize ==================== CURRENTFILESIZE=`ls -l $i | cut -d " " -f 5` ALPHAFILESIZE=`ls -l $tmp_alpha | cut -d " " -f 5` CRUSHFILESIZE=`ls -l $tmp_crush | cut -d " " -f 5` echo "OLD: $CURRENTFILESIZE" echo "ALPHA: $ALPHAFILESIZE" echo "CRUSH: $CRUSHFILESIZE" if [ "$ALPHAFILESIZE" -gt "$CRUSHFILESIZE" ]; then echo "`date +%T` Replacing $i by $tmp_crush (with alpha channel)." >> $LOGFILE mv $tmp_crush $i rm $tmp_alpha else echo "`date +%T` Replacing $i by $tmp_alpha (uncrushed)." >> $LOGFILE mv $tmp_alpha $i rm $tmp_crush fi; done exit 0