<?PHP
/*
	author:
		written by matthew rossetta (archanglmr@yahoo.com) as a project to learn php
		additional help provided by adam smith

	current parameters:
		$thubsize		= the max width or height a picture can use
		$basepath		= where on the drive the calling page is located
		$thumbpath		= the relitive path of thumbnails directory
		$fullsizepath	= the relitive path of the fullsize directory
		
	key: getimagesize()
		0 = width
		1 = height
		2 = type
		3 = width & height string
			(jpeg only)
		4 = channels (3 = RGB, 4 = CYMK)
		5 = bits per color
		
	to do:
		1.) remove any printable output
		2.) return something more useful than how many thumbs generated
		3.) write the rest of the gallery script
	
	other info:
		*only files ending in ".jpg" will be evaluated for processing to thumb nail.
		*if it's not a valid ".jpg" but is an image, no thumb nail will be created.
		*if you have ".jpg" files that are not images, no thumb nail will be created and
		you will get a nice php warning message depending on your php config file setup.
		
		+my point is, don't try and rename bitmaps and gifs to ".jpg" in hopes of having
		a thumb created.
*/

function makethumb ($thumbsize, $basepath, $thumbpath, $fullsizepath) {
/*	holds info about a picture, recycled variable uses for whatever the current picture is */
	$sizes			= array();
/*	source image and thumb image should not have the same name, especially if they are in
	the same directory. This variable places whatever the value is before the filename of
	the original as the thumb. Example: $currentthumbnail = $thumbprefix.$currentsource;
	instead of "thumb" another good name would be "small" or "tiny" */
	$thumbprefix	= "thumb";
/*	quality of thumbnail 1-100 */
	$thumbquality	= 75;
/*	source image that will be scaled and shrunk into a thumb nail. only declared here so it
	get recycled. */
	$source			= NULL;
/*	holds how many thumb nails have been generated. Instead of returning a true or false to
	see if this function did anything it can be more speciffic and tell how many thumb nails
	were created instead of typical "some" or "none" (true or false) */
	$howmany		= 0;
		
	if ($dir_ptr = opendir($basepath.$fullsizepath)) {	
		while ($filename = readdir($dir_ptr)) {
			$current_source	= $basepath.$fullsizepath.$filename;
			if (eregi(".jpg$", $filename) && ($sizes = getimagesize($current_source)) && ($sizes[2] == 2)) {
				$current_file	= $basepath.$thumbpath.$thumbprefix.$filename;
			
				/* test if needs to be resized */
				if (file_exists($current_file)) {
					$resize = FALSE;
					$thumb_sizes = getimagesize($current_file);
					
					if ($thumb_sizes[0] == $thumbsize || $thumb_sizes[1] == $thumbsize) {
						if ($thumb_sizes[0] > $thumbsize || $thumb_sizes[1] > $thumbsize)
							$resize = TRUE;
					}
					else
						$resize = TRUE;
				}
				else
					$resize = TRUE;
		
				/* resizes & make thumb here if it's needed */
				if ($resize) {
					$source = imagecreatefromjpeg($current_source);
					
					/*  (width > height) */
					if ($sizes[0] > $sizes[1]) {
						$width = $thumbsize;
						$height = ceil( ($thumbsize / $sizes[0]) * $sizes[1] );
					}
					else {
						$height = $thumbsize;
						$width = ceil(($thumbsize / $sizes[1]) * $sizes[0]);
					}
					
					$dest = imagecreate($width, $height);
					imagecopyresized ($dest, $source, 0, 0, 0, 0, $width, $height, $sizes[0], $sizes[1]);
		//			imagedestroy ($source);
					
					imagejpeg ($dest, $current_file, $thumbquality);
		//			imagedestroy ($dest);
		
					print ("<A HREF=\"$fullsizepath$filename\"><IMG BORDER=\"2\" SRC=\"$thumbpath$thumbprefix$filename\" ALT=\"resized from:\n$sizes[0] x $sizes[1]\n       to\n$width x $height\"></A>");
					$howmany++;
				}
				else {
					print ("<A HREF=\"$fullsizepath$filename\"><IMG BORDER=\"2\" SRC=\"$thumbpath$thumbprefix$filename\" ALT=\"Click to See Larger Version of: \n\t$filename\"></A>");
				}
			}
#			else
#				print ("$filename is not a valid jpeg<BR>\n");
		}
	}
	
	return $howmany;
}	?>