Sometimes you have a large number of photos to put on a web page in a short space of time. people may be hassling you to get them up ASAP, but also complaining that they're not nicely arranged and thumbnailed. Say, for example, you've offered to host a load of incriminating pics of a noder meet.

What you need it something that will generate the pages and the thumbnails for you. These two PHP scripts will do just that. Just fill a directory with the source images and it will make a page full of thumbnails which it will generate itself. See this in action at ascorbic.net, which has the pics of the aforementioned gathering.

This requires three files: thumbnail.php, which creates the thumbnails, index.php which creates the page, and .htaccess which directs request for missing thumbnails to the script that creates them.

thumbnail.php

This is run whenever someone requests a thumbnail that hasn't been created yet. It creates and returns the thumbnail, and saves a copy in the thumbnail directory.

<?php
//where are the big pics, and where do want
//to put the thumbnails?
$sourcedir=$DOCUMENT_ROOT ."/";
$outputdir=$sourcedir."/small/";
//how wide are the thumbnails to be?
//height will be proportionate
$outputwidth=100;
$filename=basename($REQUEST_URI);
$src=$sourcedir.$filename;
//load the big image
$im=imagecreatefromjpeg($src);
//find out how big it is
$sz=getimagesize($src);
//work out the height of the thumbnail
$h=($outputwidth/$sz[0])*$sz[1];
//create a new blank image for the thumbnail
$out=imagecreate($outputwidth,$h);
//copy and resize the large image to this
imagecopyresized($out,$im,0,0,0,0,$outputwidth,
$h,$sz[0],$sz[1]);
//output the image
header("Content-type: image/jpeg");
imagejpeg($out);
//save the image so we don't need to recreate
//it next time
imagejpeg($out,$outputdir.$filename);
?>

index.php

This searches the image directory for jpegs and tries to display thumbnails of each them. thumbnail.php should generate any missing thumbnails. Edit this HTML page to make it more or less pretty.

This wants to go in the same directory as the thumbnails. Make sure it points to wherever you put thumbnail.php. If you already have a .htaccess file, just add this line to it. Your httpd.conf will need to permit .htaccess files to change the error document.

A thumbnail (in webgraphics sense) is a small version of a picture file. Typical maximum sizes range from 75x75 to 150x150 pixels.

Thumbnails are used as a link to the actual image file. Often, information about the image file are provided near the thumbnail (real image size in pixels and file size in human-readable format). This is to help people with modems to judge what to download in full.

A typical mistake with thumbnails is to use construction similar to following: