Image Thumbnails

30 09 2008

So I have a directory on my server with several (more than 50) sub-directories filled with pictures. Obviously this is a lot of pictures, and since I did not name the images as I uploaded them, I have no idea what’s what. I just know where and when they were taken from the folder titles. I thought something that would generate thumbnails would be great for browsing through the pictures, especially if you were looking for a certain image. So I found this script, and added a two lines that would automatically create a thumbs directory in the Image directory, and that would redirect to the newly generated html page.

Basically what the script does is resize all the .jpg files in the directory, and saves them in the newly created thumbs directory. It then creates a file called gallery.html which shows all the new thumbnails with links to their originals. It does pretty much exactly what I was looking for. Originally the script was called create_thumbs.php, but I renamed it to index.php so it would load each time you opened the folder. In case images had been added. Then I added a redirect line so that it would automatically redirect to gallery.html. This is what I ended up with.
<?php
/*
This is the PHP code for the How to Create Thumbnail Images using PHP Tutorial
This script creates all of the thumbnail images and the gallery.html page.
Note: Make sure that PHP has permission to read and write
to the directory in which .jpg files are stored and the directory
in which you're trying to create thumbnails.
You may use this code in your own projects as long as this
copyright is left in place. All code is provided AS-IS.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Copyright 2007 WebCheatSheet.com
*/

header( 'Location: ./gallery.html' ) ;
mkdir("./thumbs", 0777);
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{

// open the directory
$dir = opendir( $pathToImages );

// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";

// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );

// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

// create a new tempopary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}

function createGallery( $pathToImages, $pathToThumbs )
{
echo "Creating gallery.html <br />";

$output = ";
$output .= "<head><title>Thumbnails</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";

// open the directory
$dir = opendir( $pathToThumbs );

$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />";
$output .= "</a></td>";

$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir( $dir );

$output .= "</tr>";
$output .= "</table>";
$output .= "<br />";
$output .= "<a href = 'http://thesupportdepartment.com/Pictures/Willem Sr Pictures'>Return To Pictures</a>";
$output .= "</body>";
$output .= "</html>";

// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}

// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("./","thumbs/",100);
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("./","thumbs/");
?>

You place it in the folder where the images are located, name it index.php, make sure the folder is chmodded to 777 and when you navigate to that folder, it will create a thumbs directory and the gallery.html file. Then it will redirect to your gallery.

Finally, since I had to place this file in a lot of directories, I placed a copy of the file in the parent directory. Changed to the parent directory, and issued the following Unix command. Then I deleted the copy located in the parent directory.

sudo for dir in *; do [ -d “$dir” ] && cp index.php “$dir” ; done

I got the script from here.