PHP Image Gallery

I wrote this this morning to dynamically generate thumbnails and display the thumbnails with links to the full sized pictures because I was tired of having to download the entire picture to see my digital photos (they’re all online; previously they were in the apache default directory viewer).

Put this in a php file in the folder and make sure the folder is chmodded to 0777 so that the script can write to the thumbnail directory that it will create.

Once the page has been accessed once (and the thumbnails are created), it will just use the previously generated files and not use up all that CPU power converting the pictures again.

Requires PHP GD library.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Thumbnails</title>
<style type="text/css">
body {
	font-family: Verdana, Arial, Helvetica, sans-serif;
}
</style>
</head>

<body>
<i><?PHP echo stripslashes($_SERVER['PHP_SELF']); ?></i><br /><br />
<?PHP
$directory = ".";

if(!file_exists("./thumbs")) {
	//If there is no "thumbs" directory, create a thumbnail directory
	mkdir("./thumbs");
}

$myDirectory = opendir($directory);
while($entryName = readdir($myDirectory)) {
	if(is_dir($entryName)) {
		if($entryName != "." && $entryName != "thumbs") {
			if($entryName == "..") {
		 	echo "<img src=\"/icons/folder.gif\" /> <a href=\"../\">Parent Directory</a><br /><br />

";
			} else {
		 	echo "<img src=\"/icons/folder.gif\" /> <a href=\"./" . $entryName . "\">" . $entryName . "</a><br />
";
			}
		}
	}
}
echo "<br />
";

closedir($myDirectory);

$myDirectory = opendir($directory);
echo "<table>
";
echo "<tr>";
$i = -1;
while($entryName = readdir($myDirectory)) {
	if($entryName != "Thumbs.db" && $entryName != "thumbnails.php" && is_file($entryName)) {
		if(strtoupper(substr($entryName, -4)) == ".JPG" || strtoupper(substr($entryName, -5)) == ".JPEG") {
			$i++;
			//If the File is a Jpeg, Check if its Thumbnail Exists
			//If it does, display it with a link to the image
			//If not, create one first.
			if(!file_exists("./thumbs/" . $entryName)) {
				thumbnail(".", "./thumbs", $entryName, 150);
			}
			if(!($i%4)) {
				echo "</tr><tr>";
			}
			echo "<td>";
			echo "<a href=\"" . $entryName . "\"><img src=\"./thumbs/" . $entryName . "\" /></a><br />
";
			echo "<a href=\"" . $entryName . "\">" . $entryName . "</a><br /><br />

";
			echo "</td>";
		} else {
			//echo $entryName . "<br /><br />

";
		}
	}
}
echo "</tr>";
echo "</table>
";
closedir($myDirectory);

function thumbnail($image_path, $thumb_path, $image_name, $thumb_width) {
	$src_img = imagecreatefromjpeg("$image_path/$image_name");
	$origw=imagesx($src_img);
	$origh=imagesy($src_img);
	$new_w = $thumb_width;
	$diff=$origw/$new_w;
	$new_h=$new_w;
	$dst_img = imagecreatetruecolor($new_w,$new_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));

	imagejpeg($dst_img, "$thumb_path/$image_name");
	return true;
} 
?>
</body>
</html>

Edit: thumbnail function is a slightly modified one from the PHP.net handbook.