<?php
function php_thumbnails($imagefolder) {
    $images = $imagefolder; 
    $thumbnails = $imagefolder."/thumbs"; 

	//load images into an array and sort them alphabeticall:
	$files = array();
	if ($handle = opendir($images)){
		while (false !== ($file = readdir($handle)))
			//Only do JPG's
			if(eregi("((.jpeg|.jpg)$)", $file))
				$files[] = array("name" => $file);
		closedir($handle);
	}
	//Obtain a list of columns

	foreach ($files as $key => $row)
		$name[$key]  = $row['name'];

	//Put images in order:
	array_multisort($name, SORT_ASC, $files);
	//set the GET variable name
	$pic = $imagefolder;

	foreach ($files as $file){
			$name = $file['name'];
			$splitname = explode (".", $name);

			$pictitle = str_replace("_"," ",$splitname[0]);
			$link = "<a rel=\"lightbox[" . $images . "]\" title=\"$splitname[0]\" href=\"" . $images . "/" . $name . "\">";
				
			if (file_exists("$thumbnails/".$splitname[0]."_thumb.jpg")){
				// Load the thumbnail image
				echo($link);
				echo("<img class=\"thumb\" src=\"" . $thumbnails . "/".$splitname[0]."_thumb.jpg\" alt=\"$pictitle\"></a> \n");
		  	} else {
				// Create a thumbnail image
				echo($link);
				echo("<img class=\"thumb\" src=\"thumbnail.php?imagefolder=" . $images . "&thumbfolder=" . $thumbnails . "&name=" . $file['name'] . "&im=" . $images .  "/" . $file['name'] . "\" alt=\"$pictitle\"></a> \n");
			}
		}
	reset($files);
	}
?>
