#! /bin/sh # Here is a script for making thumbnails and an index.html file # for a directory of JPEG images. You can always find the latest # version at http://www.molenda.com/makethumbs.sh # Run makethumbs.sh --help for a brief help message. #---------------------------------------------------------------------- # Written by Jason Molenda, jason+makethumbs@molenda.com, 1998-09-13. # This script is placed in the public domain. #---------------------------------------------------------------------- # RCS ID: $Id: makethumbs.sh,v 1.44 2001/07/14 00:11:49 molenda Exp $ # Run this script while you're in a directory with JPEG files. It will # create thumbnails, an index.html, and do a small number of common fixups # to make the directory browsable over the web. # For best results, you'll have images with names like # "Jason-before-the-game.jpg" (underbars are treated the same). # The resulting HTML file will have a name like "Jason before the game" # displayed under the thumbnail. # If you want to leave images out of the HTML index, list them in # a file called DONTSHOW.txt (list the full filenames of the images). # If you pass a list of filenames to the script on the command line, it # will only create a table for those images. Specifically, if you pass # filenames on the command line, it doesn't create any of the HTML header # or trailer bits, so it is easy to include the table in an already # extant HTML file. ################################ ### OPTION PROCESSING, HELP MENU ################################ init () { MAXTHUMBSIZE=150 MAXTHUMBHEIGHT=$MAXTHUMBSIZE MAXTHUMBWIDTH=$MAXTHUMBSIZE COLUMNS=3 SHOW_FILESIZE=0 USE_TWO_WINDOWS=0 REDUCE_BIG_PICS=1 REDUCE_TRIGGER_WIDTH=1024 REDUCE_TRIGGER_HEIGHT=768 REDUCE_WIDTH=800 REDUCE_HEIGHT=600 } help () { # The following exec goop so I don't have to manually redirect every # message to stderr in this function. exec 4>&1 # save stdout fd to fd #4 exec 1>&2 # redirect stdout to stderr cat <<__EOM__ Usage: `basename $0` [--maxthumbsize=n] [--columns=n] [--show-filesize] [--use-two-windows] [--disable-reduce] [filenames...] --maxthumbsize Maximum size of thumbnails, in pixels, default $MAXTHUMBSIZE --columns Number of thumbnails per line, default $COLUMNS --disable-reduce Don't create a reduced img if the picture is large --show-filesize Show image size in kbytes below thumbnail, default is `[ $SHOW_FILESIZE -eq 1 ] && echo enabled || echo disabled` --use-two-windows Bring up a new window to see images, default is `[ $USE_TWO_WINDOWS -eq 1 ] && echo enabled || echo disabled` Run this script in a directory of JPEG files to create thumbnail images and an index.html. If an index.html is already present, its HTML is sent to stdout. You may list the JPEG files on the command line if you prefer. By default, `basename $0` works on every JPEG file in the current working directory. This script written by Jason Molenda, jason+makethumbs@molenda.com. It is placed in the public domain. This is the version string: '\$Id: makethumbs.sh,v 1.44 2001/07/14 00:11:49 molenda Exp $' The latest version of this script is always available at http://www.molenda.com/makethumbs.sh __EOM__ exec 1>&4 # Copy stdout fd back from temporary save fd, #4 } process_options () { while [ $# -gt 0 ] do optarg="`echo $1 | sed 's,^[^=]*=,,'`" case "$1" in --maxthumbsize=* | --max-thumb-size=*) MAXTHUMBSIZE="$optarg" MAXTHUMBHEIGHT=$MAXTHUMBSIZE MAXTHUMBWIDTH=$MAXTHUMBSIZE ;; --maxthumbheight=* | --max-thumb-height=*) MAXTHUMBHEIGHT="$optarg" ;; --columns=* | --cols=*) COLUMNS="$optarg" ;; --show-file*) SHOW_FILESIZE=1 ;; --use-two-windows*) USE_TWO_WINDOWS=1 ;; --disable-reduce) REDUCE_BIG_PICS=0 ;; --enable-reduce) REDUCE_BIG_PICS=1 ;; --reduce-height=*) REDUCE_HEIGHT="$optarg" ;; --reduce-width=*) REDUCE_WIDTH="$optarg" ;; --reduce-trigger-height=*) REDUCE_TRIGGER_HEIGHT="$optarg" ;; --reduce-trigger-width=*) REDUCE_TRIGGER_WIDTH="$optarg" ;; -h | --help | -v | --version | -V) help exit 1 ;; -*) echo "`basename $0`: ERROR: Unrecognized option \"$1\"" >&2 ;; *) files="$files $optarg" ;; esac shift done } find_needed () { for i in djpeg cjpeg pnmscale do found=0 for j in `echo $PATH | sed -e 's,^:,. ,' -e 's,:, ,g'` do if [ -x $j/$i ] then found=1 break fi done if [ $found -ne 1 ] then echo ERROR: Could not find needed utility \"${i}\". Aborting. >&2 if [ $i = djpeg -o $i = cjpeg ] then echo ERROR: You may be able to find a copy of this at ftp://ftp.uu.net/graphics/jpeg >&2 fi if [ $i = pnmscale ] then echo ERROR: You need to install the \"pbmplus\" utilities or \"netpbm\" utilities. >&2 echo ERROR: You may be able to find this at >&2 echo ERROR: http://netpbm.sourceforge.net/ >&2 echo ERROR: ftp://metalab.unc.edu/pub/linux/apps/graphics/convert/ >&2 echo ERROR: ftp://ftp.x.org/contrib/utilities/netpbm-1mar1994.p1.tar.gz >&2 fi echo ERROR: >&2 echo ERROR: You will find all the necessary utilities pre-installed >&2 echo ERROR: on most Linux systems. >&2 exit 1 fi done } cleanup () { trap "rm -f $TMPFILE $1; exit 1" 0 1 2 15 } ############################### ### MAIN SCRIPT ############################### init find_needed process_options $* # this script is for making publically-accessable HTML pages; therefore # the following is always correct (for me, anyway :-). chmod a+x . TMPFILE=/tmp/makethumbs.$$ cleanup dirname="`pwd | sed -e 's,^.*/,,' -e 's,_, ,g'`" # Is the user passing filenames on the command line? if [ -n "$files" ] then FILELIST="$*" HEADER=0 else FILELIST=`ls -1 | egrep '\.jpg$|\.JPG$|\.JPEG$'` HEADER=1 if [ ! -f index.html ] then exec > index.html fi fi if [ -z "$FILELIST" ] then echo Error! No JPEG \(\"foo.jpg\"\) files in this directory! Exiting. >&2 exit 1 fi ## Move any ".JPG", ".jpeg", ".JPEG" files to ".jpg" rm -f $TMPFILE ls -1 | egrep '\.JPG$|\.jpeg$|\.JPEG$' > $TMPFILE if [ -s $TMPFILE ] then while read i do j=`echo "$i" | sed -e 's,.JPG$,.jpg,' -e 's,.jpeg$,.jpg,' -e 's,.JPEG$,.jpg,'` if [ ! -f "$j" ] then mv "$i" "$j" FILELIST=`echo $FILELIST | sed "s,${i},${j},"` fi done < $TMPFILE fi rm -f $TMPFILE if [ $HEADER -eq 1 ] then echo '' echo '' echo " $dirname" echo '' echo '' echo "" echo "

$dirname

" echo "" fi echo '' c=0 first=1 for i in $FILELIST do if echo "$i" | egrep -i '\-r\.jpg$|\-t\.jpg$' >/dev/null 2>&1 then continue fi if grep "$i" DONTSHOW.txt >/dev/null 2>&1 then continue fi chmod a+r "$i" chmod a-x "$i" i_thumbnail=`echo "$i" | sed -e 's,.JPG$,-t.jpg,' -e 's,.jpg$,-t.jpg,'` i_basename=`echo "$i" | sed -e 's,.JPG$,,' -e 's,.jpg$,,'` if [ $c -eq 0 ] then if [ $first -eq 1 ] then echo "" echo '' first=0 else echo '' echo '' echo "" echo "" echo "" echo '' fi echo ' ' echo ' ' echo '' echo "" echo '
 
' echo -n ' ' else echo '' echo -n ' ' fi if [ ! -f "$i_thumbnail" ] then cleanup "$i_thumbnail" djpeg -ppm < "$i" | pnmscale -xysize $MAXTHUMBWIDTH $MAXTHUMBHEIGHT | cjpeg -optimize > "$i_thumbnail" cleanup "" chmod a+r "$i_thumbnail" fi thumb_info=`djpeg -ppm < "$i_thumbnail" | pnmfile` thumb_width=`echo $thumb_info | awk '{print $4}'` thumb_height=`echo $thumb_info | awk '{print $6}'` title=`echo "$i_thumbnail" | sed -e 's,-t.jpg,,' -e 's,-, ,g' -e 's,_, ,g'` PIC_WAS_REDUCED=0 i_reduced=`echo "$i" | sed -e 's,.JPG$,-r.jpg,' -e 's,.jpg$,-r.jpg,'` if [ $REDUCE_BIG_PICS -eq 1 -a -f "$i_reduced" ] then PIC_WAS_REDUCED=1 i_reduced_orig_file="$i" i="$i_reduced" else if [ $REDUCE_BIG_PICS -eq 1 ] then rm -f $TMPFILE djpeg -ppm < "$i" > $TMPFILE pic_info=`pnmfile < $TMPFILE` pic_width=`echo $pic_info | awk '{print $4}'` pic_height=`echo $pic_info | awk '{print $6}'` if [ "$pic_width" -ge $REDUCE_TRIGGER_WIDTH -o \ "$pic_height" -ge $REDUCE_TRIGGER_HEIGHT ] then PIC_WAS_REDUCED=1 i_reduced=`echo "$i" | sed -e 's,.JPG$,-r.jpg,' -e 's,.jpg$,-r.jpg,'` cleanup "$i_reduced" cat $TMPFILE | pnmscale -xysize $REDUCE_WIDTH $REDUCE_HEIGHT | cjpeg -optimize > "$i_reduced" cleanup "" chmod a+r "$i_reduced" chmod a-x "$i_reduced" i_reduced_orig_file="$i" i="$i_reduced" fi rm -f $TMPFILE fi fi if [ $SHOW_FILESIZE -eq 1 ] then bytes=`ls -l "$i" | awk '{print $5}'` kbytes=`expr $bytes / 1024` fi i=`echo "$i" | sed 's, ,%20,g'` i_thumbnail=`echo "$i_thumbnail" | sed 's, ,%20,g'` echo -n "\"\"
${title}
" if [ $SHOW_FILESIZE -eq 1 ] then echo -n " (${kbytes}k)" fi echo "" c=`expr $c + 1` if [ $c -eq $COLUMNS ] then c=0 fi if [ "$REDUCE_BIG_PICS" -eq 1 -a "$PIC_WAS_REDUCED" -eq 1 ] then echo -n "
[Large version]" bytes=`ls -l "$i_reduced_orig_file" | awk '{print $5}'` kbytes=`expr $bytes / 1024` echo -n " (${kbytes}k)" fi for type in png PNG tif tiff TIF TIFF do if [ -f "${i_basename}.${type}" ] then type_big=`echo $type | tr '[a-z]' '[A-Z]'` echo -n "
[${type_big} format]" bytes=`ls -l "${i_basename}.${type}" | awk '{print $5}'` kbytes=`expr $bytes / 1024` echo -n " (${kbytes}k)" break fi done done echo '
' if [ $HEADER -eq 1 ] then echo "" echo "

This page created on `date '+%Y-%m-%d'`." echo "" echo '' echo '' echo '' echo '' echo '' echo '' echo "" echo '' echo '' fi chmod a+r index.html exit 0