Quick and Easy JPG Thumbnailing


Everyone likes to put images on their website, me included. However, some images are a bit large, and thismay slow down the download when displaying a page of a bunhc of images. Hence the need for thumbnails. Giving a page of thumbnails, with links to the actual image can let the user decide which image to view if he/she chooses. My Linux box is only accessible from the console, so no X programs can run off it. To create thumbnails for each image, my first thought was to: I found this to be laborious especially with alot of images. So I thought of a way to systematiclly create thumbnails of my .jpg file from the console of the web server box.
JPEG tools
The Independent JPEG Group has great tools to manipulate .jpg files. These tools are usually included in most Linux distributions. Using these tools, I wrote a script to: ..eliminating the need and fuss for GUI type image programs, especially where a console prohibits X applications. Here's how the script works:

This way, image scaling can be automated for any number of images of type .jpg.

     1	#!/bin/sh
     2	
     3	# NAME   : mkthumbs - script to scale images down
     4	#
     5	# USAGE  : mkthumbs [dir] [scalefac]
     6	#
     7	# INPUTS : [dir]      -> the directory with jpg files
     8	#          [scalefac] -> the factor to scale them down by
     9	#
    10	# OUTPUTS: scaled down .thumb.jpg files of same basename
    11	#
    12	# EXAMPLE: to scale down your cwd .jpg's by a factor of 8:
    13	#          % ./mkthumbs . 8
    14	#
    15	# $Id: mkthumbs,v 1.0 2001.01.28 14:50:59 tkralidi Exp $
    16	
    17	if [ $# -ne 2 ]; then
    18	  echo "Usage: $0 [dir] [scalefac]"
    19	  exit 1
    20	fi
    21	
    22	DJPEG="/usr/bin/djpeg"
    23	CJPEG="/usr/bin/cjpeg"
    24	
    25	echo "Working on .jpg files in $1"
    26	
    27	for i in $1/*.jpg; do
    28	  if [ -f $i ]; then
    29	    echo "Scaling $i by $2"
    30	    $DJPEG -scale 1/$2 $i | $CJPEG > ${i%jpg}thumb.jpg
    31	    echo "${i%jpg}thumb.jpg created"
    32	  else
    33	    echo "No files in $1 of type .jpg"
    34	    echo "Exiting"
    35	    exit 2
    36	  fi
    37	done
    38	
    39	echo "Done"
    40	
    41	exit 0