#! /bin/sh resize=0 thumb=0 quality=85 showHelp() { echo "-------------------------------------" echo " resize images and create thumbnails " echo "-------------------------------------" echo "Usage: `basename $0` -r #x# -t -f " echo "" echo "Options:" echo " -r #x# = Rescale the image. WIDTHxHEIGHT. Optional." echo " -q # = Specify the JPEG quality. Optional. Default is 85%." echo " -t = Create 120x120 thumbnails" echo " -h = Show this help." echo "" echo "Example:" echo " `basename $0` -r 1024x768 -f ~/Sites/webphotos" } verifyResult () { if [ $1 -ne 0 ]; then echo "Error during last operation. Exiting" exit $1 fi } GetSize () { # get file size of passed file var /usr/local/bin/du -sh "${1}" | awk '{print $1}' } GetType () { # get file type of passed file var file "${1}" | awk -F : '{print $2}' | awk '{print $1}' } makeThumb() { echo -n "Making Thumbnail for ${1}..." convert -quality 60 -size 120x120 "${1}" -resize 120x120 +profile "*" "thumbs/${1}" echo "done" } rescaleImg() { echo -n "Rescaling Image ${1}: " oldsize=$(GetSize "${1}") echo -n "${oldsize} -> " mogrify -size ${2} -resize ${2} -quality ${3} +profile "*" "${1}" newsize=$(GetSize "${1}") echo "${newsize}" } if [ $# -eq 0 ] ; then echo "No valid arguments" showHelp exit elif [ $1 = "-h" ] ; then showHelp exit fi while [ $# -ne 0 ]; do if [ $# -eq 0 ] ; then echo "No valid arguments" showHelp exit elif [ $1 = "-h" ] ; then showHelp exit elif [ $1 = "-r" ] ; then resize=1 scale=${2} shift shift elif [ $1 = "-q" ] ; then quality=${2} shift shift elif [ $1 = "-t" ] ; then thumb=1 shift else while [ $# -ne 0 ]; do folder=${1} filetype=$(GetType "${folder}") if [ $filetype = "JPEG" ]; then (verifyResult $?) if [ ${thumb} -eq 1 ] ; then mkdir -p "thumbs" (makeThumb "${folder}") (verifyResult $?) fi if [ ${resize} -eq 1 ] ; then (rescaleImg "${folder}" ${scale} ${quality}) (verifyResult $?) fi elif [ $filetype = "directory" ]; then cd "${folder}" (verifyResult $?) if [ ${thumb} -eq 1 ] ; then mkdir -p "thumbs" (verifyResult $?) fi for wfile in * do isjpeg=$(GetType "${wfile}") if [ $isjpeg = "JPEG" ]; then if [ ${thumb} -eq 1 ] ; then (makeThumb "${wfile}") (verifyResult $?) fi if [ ${resize} -eq 1 ] ; then (rescaleImg "${wfile}" "${scale}") (verifyResult $?) fi fi done fi shift done fi done