#!/bin/bash ###################################################################### # Program: mkthumbnail # Purpose: Generates HTML code for thumbnails. # Arguments: See usage. # Author: Perette Barella # Version: @(#) $Id: mkthumbnails,v 1.16 2008/12/12 21:26:07 perette Exp $ #--------------------------------------------------------------------- ###################################################################### # Function: usage # Purpose: Displays the usage of this command. # Author: Perette Barella #--------------------------------------------------------------------- function usage { echo "Usage: $arg0 [options] images-per-line max-width max-height [file ....]" echo " $arg0 [options] images-per-line max-width max-height [file ...]" echo "Options:" echo " -c file Read captions from file" echo " -o Insert image in order listed in caption file." echo " -s attributes Create single image and include specified HTML attributes" echo " Images-per-line is left out when using -s option." echo " -q percent Image quality, 1 = crap and 99=great" exit 1 } ##### End of function usage ##### ###################################################################### # Function: parse_arguments # Purpose: Parses the command line arguments # Arguments: The command line arguments. # Returns: Number of arguments parsed. # Author: Perette Barella #--------------------------------------------------------------------- function parse_arguments { typeset option single=false captions="" ordered=false comments="" quality=50 while getopts 's:oc:q:?' option do case "$option" in c) captions="$OPTARG" if [ ! -e "$captions" ] then echo "`pwd`/$captions: caption file not found." 1>&2 exit 1 fi ;; o) ordered=true ;; s) single=true comments="$OPTARG" ;; q) quality="$OPTARG" if ! expr "$quality" : '[0-9]\{1,2\}$' >/dev/null then usage fi ;; *) usage ;; esac done if $ordered && [ "$captions" = "" ] then echo "$arg0: Need provide caption file for ordered thumbnails" 1>&2 exit 1 fi return $((OPTIND - 1)) } ##### End of function parse_arguments ##### ###################################################################### # Function: resize_this # Purpose: Calculates the destination size given the actual # image sizes. Generates the HTML. # Arguments: $1 - width # $2 - height # $3 - filename # Returns: 0 on success, non-0 on failure. # newwidth, newheight - size the image must result in. # Author: Perette Barella #--------------------------------------------------------------------- function resize_this { typeset x="$1" y="$2" file="$3" thumbfile="$4" status=0 alternate="" if ! expr "$x $y" : "[0-9][0-9]* [0-9][0-9]*\$" >/dev/null then echo "$arg0: width or height of $file screwed up: $x" 1>&2 return 1 fi # calculate the adjustment for width and height as a per thousand. adjwidth=9999 adjheight=9999 [ "$destwidth" != "-" ] && let adjwidth="(destwidth * 1000) / $x" [ "$destheight" != "-" ] && let adjheight="(destheight * 1000) / $y" typeset adjust=$adjwidth [ $adjheight -lt $adjwidth ] && let adjust=$adjheight let newwidth="(x * adjust) / 1000" let newheight="(y * adjust) / 1000" if $single then echo "_IMAGE(\`\`$thumbfile'', \`\`$comments'')" | sed 's/"/\"/g' else alternate="" if [ "$captions" != "" ] then alternate=$(grep "^$file" "$captions" | cut -f2- | head -1) fi if [ "$alternate" = "" ] && ! expr "$file" : "\(.*/\)\{0,\}[A-Z0-9]\{0,3\}[0-9][0-9]*\.[a-z]*\$" >/dev/null then alternate=$(echo "$file" | sed -e 's/[0-9]*\.[a-z][a-z]*$//g' \ -e 's&/&: &' -e 's/[^:a-zA-Z0-9]/ /'g) fi [ "$alternate" = "" ] && alternate="Thumbnail." [ $count -eq 0 ] && echo "