<?PHP
/*
	author:
		written by matthew rossetta (archanglmr@yahoo.com) as a project to learn php
		additional help provided by adam smith (regular expression in eregi())

	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:
		[ ] remove any printable output
		[X] return something more useful than how many thumbs generated
		[ ] 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 imagecmp ($one, $two) {
	return strcmp($one->s_name, $two->s_name);
}

function makethumb ($thumbsize, $basepath, $thumbpath, $fullsizepath) {
/*	class def needed for image object type */
	include_once ("image_class.php");
/*	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. If I move imagecreatefromjpeg() into imagecopyresized() I can avoid declaring
	$source completely */
	$source			= NULL;
/*	this is an array of image objects that will be returned by this function */
	$gallery		= array();
/*	standard counter variable. thought there is no "for" loop, $i is still needed for referenceing
	the current cell of the array and assigning it an image object */
	$i				= 0;

	if ($dir_ptr = opendir($basepath.$fullsizepath)) {	
		while ($filename = readdir($dir_ptr)) {
		
			$current_source	= $basepath.$fullsizepath.$filename;
			/* change this if statment to something better so that getimagesize() is not called twice */
			if (eregi(".jpg$", $filename) &&
				($sizes = getimagesize($current_source)) &&
				($sizes[2] == 2)) {
				/* new image object added to the gallery array */
				$gallery[$i] = new image;
				/* basic set up for image objects */
				$gallery[$i]->set_paths($basepath, $thumbpath, $fullsizepath);
				/* sets up the source image (aka: full sized image) */
				$gallery[$i]->set_s_info($filename);

				/* resizes & make thumb here if it's needed */
				if (!$gallery[$i]->thumb_exist($thumbprefix.$filename, $thumbsize)) {
					$source = imagecreatefromjpeg($current_source);
					$gallery[$i]->calc_t_dims($thumbsize);
					$dest = imagecreatetruecolor($gallery[$i]->t_width, $gallery[$i]->t_height);
					imagecopyresized ($dest, $source, 0, 0, 0, 0, $gallery[$i]->t_width, $gallery[$i]->t_height, $gallery[$i]->s_width, $gallery[$i]->s_height);
					imagejpeg ($dest, $gallery[$i]->basepath.$gallery[$i]->t_path.$gallery[$i]->t_name, $thumbquality);
				}
				$i++;
			} /* image check if statment*/
		} /* get file loop */
	/* cleans up loose ends because it's good programming */
	if ($source)
		imagedestroy ($source);
	closedir($dir_ptr);
	} /* open gallery directory if statment*/
	usort($gallery, "imagecmp");
	
	return $gallery;
}	?>