Location: PHPKode > scripts > ImageArchive > imagearchive/classImageArchive.php
<?php

/* 
	CLASS ImageArchive
	
	Lets you browse a directory structure for jpegs and pngs and view/rotate images
	on the fly.

	*** *** *** You need GD installed for this to work! *** *** *** 

	*** *** *** You must set the mandatory variables for this to work! *** *** *** 

	Simply set the mandatory variables, and call the Go() function, which MUST be before the
	page headers (it sets a cookie).  If you want to place the tumbnail HTML inside your own 
	page structure, call use $my_html = Go(false); then print $my_html where you want in your
	page.

	The class is a bit of a mess because I built it "on the fly" so to speak.
	
	=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+

	Feel free to distribute this.

	If you use this or have any commments, please let me know at 
	thomaswp[a]totalise.co.uk.

	=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+

*/

class ImageArchive {
    
	// Mandatory variables
	var $ARCHIVE_ROOT = null;			// Path to the root of the archive.  Default is root folder
	var $VIRTUAL_ROOT = null;			// Path to the root of the archive on the web server.  Default is current folder
	var $ARCHIVE_THUMBNAIL_ROOT = null;	// Path to where the thumbnails will be stored (cannot be the same as $ARCHIVE_ROOT)
	var $VIRTUAL_THUMBNAIL_ROOT = null;	// Path to where the thumbnails will be stored on the web server (cannot be the same as $VIRTUAL_ROOT)

	// Optional variables
	var $IMAGEARCHIVE_TITLE = "Image Archive";	// Title for this archive
	var $JPEG_QUALITY = 100;	// Jpeg quality of thumbnails / images
	var $IMAGE_TYPES_TO_DISPLAY = "jpg|jpeg|png";	// Must be delimited with | and must only be jpeg, jpg or png
	var $LIST_ITEM_LEADER = "";	// Text before the folder/image lists	
	var $LIST_ITEM_FOLLOWER = "";	// Text after the folder/image lists 
	var $TIME_TO_CREATE_ONE_THUMBNAIL = 5;	// depends on the speed of the server - mine is old P100, so I have to use 20!
	var $MESSAGE_UP = "^ Up";	// Message to go up one directory when browsing
	var $IMAGE_LINK_BORDER = 0;	// Width of the border on an image link
	var $MAX_DIMENSION = 640;	// Maximum size of image when first viewing it, -1 for full size (which is dangerous for bandwidth
	var $MAX_THUMBNAIL_DIMENSION = 100;	// Maximum width of a thumbnail
	var $THUMBNAILS_IN_ROW = 5;	// Number of thumbnails in a row
	var $SIZE_DELIMITER = " x ";	// goes between width and height on image e.g. 640 x 480 (and used later)

	// Directory list formatting
	var $THUMBNAIL_DIR_LIST_TABLE_PROPERTIES =		/* text in the <TABLE> tag for directory listing */
			"border=0";
	var $THUMBNAIL_DIR_LIST_CELL_PROPERTIES =			/* text in the <TD> tag in directory listing */
			"style=\"align: left; font-size: 0.7em;\""; 
	var $THUMBNAIL_PICTURE_TABLE_PROPERTIES = 		/* text in the <TABLE> tag for thumbnails */
			"border=0";
	var $THUMBNAIL_PICTURE_TD_PROPERTIES = 			/* text in the <TD> tag for thumbnails */
			"align=center valign=bottom bgcolor=gray";
	var $THUMBNAIL_PICTURE_DIV_PROPERTIES =			/* text in <DIV> tags in thumbnail cells */
			"style=\"align: center; font-size: 0.7em; color: white\"";
	var $THUMNAIL_PICTURE_SELECT_PROERTIES =			/* text in <SELECT> tags in thumbnail cells */
			"style=\"align: center; font-size: 1em; color: blue\"";
	var $SIZE_MESSAGE = "Maximum dimension of image when clicked:";		// Message for the resize box
	var $COOKIE_LIFETIME_MINUTES = 5;					// length of time a cookie lasts
	var $ROTATE_ANTICLOCKWISE_90_HTML = "A90";		// HTML for text indicating rotation
	var $ROTATE_ANTICLOCKWISE_TEXT = "Rotate anti-clockwise 90 degrees and view.";	// Text for rotating 90 degrees
	var $ROTATE_CLOCKWISE_90_HTML = "C90";				// HTML for text indicating rotation
	var $ROTATE_CLOCKWISE_TEXT = "Rotate clockwise 90 degrees and view.";	// Text for rotating 90 degrees

	// Page settings
	var $IMAGES_BEFORE_MESSAGE_APPEARS = 3;				//Number of thumbnails to create before message (below) appears.
	var $MESSAGE_MANY_THUMBS_TO_CREATE_LEADER =			/* number of images warning */
			"Creating thumbnails can take a long time, and there \\nmay be as many as ";
	var $MESSAGE_MANY_THUMBS_TO_CREATE_FOLLOWER =			/* number of images warning */
			" thumbnails to create.  \\nPlease be patient...";
	var $STYLESHEET_HREF = "";		// for external stylesheet reference if required


	var $DEBUGGING = false;		// set to true to debug

	
	
	function Go($printpage = true) 
	{
		if (strlen($_POST["theImage"]) > 0)
		{ // we are going to output an image
			$this->thumbnail_master_image();
		} 
		else
		{ // we are going to output a table of thumbnails
			if ($printpage)
			{
				$answer = $this->_pageHeader();
				$answer .= $this->_pageBodyStart();
				$answer .= $this->thumbnails_as_table();
				$answer .= $this->_pageBodyEnd();
				print $answer;
				flush();
			}
			else
			{
				return $this->thumbnails_as_table();
			}
		}
	}

	function thumbnail_master_image()
	{
		$theImage =		$_POST["theImage"];
		$dest_width =	$_POST["width"];
		$dest_height = $_POST["height"];
		$degrees =		$_POST["degrees"];

		if ($degrees == '') { $degrees = 0; }

		// Load and manipulate the image
		$outputImage = $this->LoadImage($theImage);
		$outputImage = $this->ResizeImage($outputImage, $dest_width, $dest_height);
		$outputImage = $this->RotateImage($outputImage, $degrees);
		$this->Image_on_Fly ($outputImage); 

	}

	function thumbnails_as_table() 
	{
	
		$answer = $this->ImageArchiveScripts();

		// determine the dimension of the main image link
		// MUST do this before anything is output
		$max_dimension = (int)$_POST['max_dimension'];
		if ($max_dimension == 0)
		{
			$max_dimension = (int)$_COOKIE["max_dimension"]; ;
		}
		if ($max_dimension == 0)
		{
			$max_dimension = $this->MAX_DIMENSION;
		}

		$this->MAX_DIMENSION = $max_dimension;
		setcookie ("max_dimension", $this->MAX_DIMENSION, time()+60*$this->COOKIE_LIFETIME_MINUTES);


		// work out the new path
		if (strlen($_POST['parentfolder']) > 1)
		{
			$parentFolder = stripslashes($_POST['parentfolder']);
			$this->ARCHIVE_ROOT .= $parentFolder."/";
			$this->VIRTUAL_ROOT .= $parentFolder."/";
			$this->VIRTUAL_ARCHIVE_ROOT .= $parentFolder."/";
		}
		if (strlen($_POST['folder']) > 1)
		{
			$thisFolder = stripslashes($_POST['folder']);
			if (is_null($parentFolder))
			{
				$parentFolder .= $thisFolder;
			} 
			else
			{
				$parentFolder .= "/".$thisFolder;
			}
			$this->ARCHIVE_ROOT .= $thisFolder."/";
			$this->VIRTUAL_ROOT .= $thisFolder."/";
			$this->VIRTUAL_ARCHIVE_ROOT .= $thisFolder."/";
		}

		$parentFolder = stripslashes($parentFolder);
		$fcount = 0;
		$folders = $this->ImagesInDirectory($this->ARCHIVE_ROOT);

		// show the folders
		$answer .= "<H2>".stripslashes($parentFolder)."</H2>\n";
		$answer .=  "<TABLE $this->THUMBNAIL_DIR_LIST_TABLE_PROPERTIES>\n";
		$answer .=  "<FORM name=folderup action=\"".$_SERVER['SCRIPT_NAME']."\" method=post>\n";
		$answer .=  "<TR><TD $this->THUMBNAIL_DIR_LIST_CELL_PROPERTIES>\n";
		// the up button needs to post a form
		$upFolders = split("/",$parentFolder);
		$numFolders = count($upFolders);
		switch ($numFolders)
		{
		case 0:
			$upParentFolder = "";
			$upFolder = "";
			break;

		case 1:
			$upParentFolder = "";
			$upFolder = "";
			break;

		case 2:
			$upParentFolder = "";
			$upFolder = stripslashes($upFolders[0]);
			break;

		default;
			for ($uf=0;$uf<($numFolders-2);$uf++)
			{
				$upParentFolder .= $upFolders[$uf]."/";
			}
			$upParentFolder = stripslashes(substr($upParentFolder,0,strlen($upParentFolder)-1));
			$upFolder = stripslashes($upFolders[$numFolders-2]);
		}
		$answer .=  "<INPUT type=hidden name=parentfolder value=\"$upParentFolder\">\n";
		$answer .=  "<INPUT type=hidden name=folder value=\"$upFolder\">\n";
		$answer .=  "<A href=\"JavaScript:submitme(document.folderup);\">".$this->MESSAGE_UP."</A>\n";
		$answer .=  "</TD></TR>\n";
		$answer .=  "</FORM>\n";
		foreach ($folders as $folder) 
		{
			if (is_null($folder[2]))
			{ // it is a folder
				$answer .=  "<FORM name=folder$fcount action=\"".$_SERVER['SCRIPT_NAME']."\" method=post>\n";
				$answer .=  "<TR><TD $this->THUMBNAIL_DIR_LIST_CELL_PROPERTIES>\n";
				$answer .=  "<INPUT type=hidden name=parentfolder value=\"$parentFolder\">\n";
				$answer .=  "<INPUT type=hidden name=folder value=\"".str_replace("<!---->","",$folder[1])."\">\n";
				$answer .=  "<A href=\"JavaScript:submitme(document.folder$fcount);\">".str_replace("<!---->","",$folder[1])."</A>\n";
				$answer .=  "</TD></TR>\n";
				$answer .=  "</FORM>\n";
				$fcount++;
			}
		}
		$answer .=  "</TABLE>\n";

		// get all the images
		$images = $this->thumbnails_as_array(false);
		$numImages = count($images);

		if ($numImages < $this->THUMBNAILS_IN_ROW && $numImages > 0)
		{
			$this->THUMBNAILS_IN_ROW = $numImages;
		}

		// how many rows?
		$rows = ceil($numImages / $this->THUMBNAILS_IN_ROW);

		// add blank array items for number of empty cells
		$emptycells = ($rows * $this->THUMBNAILS_IN_ROW) - $numImages;
		for ($i = 1; $i <= $emptycells; $i++) 
		{
			$images[][0] = "&nbsp;";
			$images[count($images)-1][1] = "&nbsp;";
		}

		$numImages = count($images);
		$icount = 0;
		if ($numImages > 0)
		{
			$answer .= "<TABLE $this->THUMBNAIL_PICTURE_TABLE_PROPERTIES>\n";
			for ($r = 1; $r <= $rows; $r++) 
			{
				$answer .= "\t<TR>\n";
				for ($c = 1; $c <= $this->THUMBNAILS_IN_ROW; $c++) 
				{
					// Show the thumbnail with a link to the original image (resized on fly)
					$answer .= "\t\t<TD $this->THUMBNAIL_PICTURE_TD_PROPERTIES>\n";
					// work out the sizes
					$dimensions = split($this->SIZE_DELIMITER,$images[(($r-1) * $this->THUMBNAILS_IN_ROW)+($c-1)][1]);
					if (count($dimensions) == 2) 
					{ //we have two dimensions - should be width and height
						$new_width = $dimensions[0];
						$new_height = $dimensions[1];
						if ($this->MAX_DIMENSION > -1) /* full size image required! */
						{
							if ($new_width /* width */ > $new_height /* height */)
							{
								$new_height = (int)(($this->MAX_DIMENSION * $new_height) / $new_width);
								$new_width = $this->MAX_DIMENSION;
							}
							else
							{
								$new_width = (int)(($this->MAX_DIMENSION * $new_width) / $new_height);
								$new_height = $this->MAX_DIMENSION;
							}
						}
						$answer .= "<FORM method=post action=".$_SERVER['PHP_SELF']." name=iform$icount>\n";
						$answer .= "<DIV $this->THUMBNAIL_PICTURE_DIV_PROPERTIES>\n";
						$answer .= "<INPUT type=image src=\"".$images[(($r-1) * $this->THUMBNAILS_IN_ROW)+($c-1)][4]."\"\n";
						$answer .= " onMouseOver=\"window.status='".$images[(($r-1) * $this->THUMBNAILS_IN_ROW)+($c-1)][5]."'\"\n";
						$answer .= " >\n";
						$answer .= "<INPUT type=hidden name=theImage value=\"".stripslashes($images[(($r-1) * $this->THUMBNAILS_IN_ROW)+($c-1)][3])."\">\n";
						$answer .= "<INPUT type=hidden name=width value=$new_width>\n";
						$answer .= "<INPUT type=hidden name=height value=$new_height>\n";
						$answer .= "<INPUT type=hidden name=degrees value=0>\n";
						//$answer .= "&width=$new_width&height=$new_height&degrees=$new_rotate\">\n";
						//$answer .= $images[(($r-1) * $this->THUMBNAILS_IN_ROW)+($c-1)][0]."</A>
						$answer .= "</DIV>";
						$answer .= "<DIV $this->THUMBNAIL_PICTURE_DIV_PROPERTIES>\n";
						$answer .= "<A title=\"$this->ROTATE_ANTICLOCKWISE_TEXT\"";
						$answer .= " href=\"JavaScript:sizeandsubmitme(document.iform$icount,90);\">".$this->ROTATE_ANTICLOCKWISE_90_HTML."</A>\n";
						$answer .= $images[(($r-1) * $this->THUMBNAILS_IN_ROW)+($c-1)][1]."\n";
						$answer .= "<A  title=\"$this->ROTATE_CLOCKWISE_TEXT\"";
						$answer .= " href=\"JavaScript:sizeandsubmitme(document.iform$icount,270)\">".$this->ROTATE_CLOCKWISE_90_HTML."</A>\n";
						$answer .= "</DIV>\n";
						$answer .= "</FORM>";
						$icount++;
				}
					$answer .= "\t\t</TD>\n";
				}
				$answer .= "\t</TR>\n";
			}
			$answer .=  "<TR>\n<TD $this->THUMBNAIL_PICTURE_TD_PROPERTIES colspan=$this->THUMBNAILS_IN_ROW>\n";
			$answer .= "<DIV $this->THUMBNAIL_PICTURE_DIV_PROPERTIES>\n";
			$answer .=  "<FORM method=post action=\"".$_SERVER['PHP_SELF']."\">\n";
			// need the folder details for posting this form
			$parentFolder = stripslashes($parentFolder);
			$this->_debug("\$parentFolder=$parentFolder");
			$folder = substr(strrchr($parentFolder, "/"), 1);
			$this->_debug("\$folder=$folder");
			$parentFolder = stripslashes(substr($parentFolder, 0, strlen($parentFolder)-strlen($folder)));
			$this->_debug("\$parentFolder=$parentFolder");
			/*
			$folder = substr(strrchr($parentFolder, "/"), 1);
			$this->_debug("\$folder=$folder");
			*/
			if (substr($parentFolder,strlen($parentFolder)-1,1) == "/")
			{
				$parentFolder = substr($parentFolder,0,strlen($parentFolder)-1);
			}
			$this->_debug("\$parentFolder=$parentFolder");
			$answer .=  "<INPUT type=hidden name=parentfolder value=\"$parentFolder\">\n";
			$answer .=  "<INPUT type=hidden name=folder value=\"$folder\">\n";
			$answer .=  "$this->SIZE_MESSAGE  \n<SELECT name=max_dimension onChange=\"this.form.submit()\" $this->THUMNAIL_PICTURE_SELECT_PROERTIES>\n";
			$mysizes = array (array("320","320 pixels"),
									array("480","480 pixels"),
									array("640","640 pixels (default)"),
									array("800","800 pixels"),
									array("-1","Full size (beware!)"));
			foreach($mysizes as $mysize)
			{
				$answer .=  "<OPTION value=\"".$mysize[0]."\" ";
				if ($this->MAX_DIMENSION == $mysize[0])	
				{ 
					$answer .= "selected";
				} 
				$answer .=  ">".$mysize[1]."</OPTION>\n";
			}
			$answer .= "</SELECT>\n";
			$answer .= "</FORM>\n";
			$answer .= "</DIV>\n";
			$answer .= "</TD></TR>\n";
			$answer .= "</TABLE>\n";
		}


		return $answer;
		
	}


	// Creates thumbnails if required, and returns an array of <IMG> tags
	// Slow the first time round (because making images), but fast thereafter
	function thumbnails_as_array($showFolders=true) {

		$this->_CanProceed();  //checks that the variables that need to be set are set

		$answer = array(); //initialise the answer

		$images = $this->ImagesInDirectory($this->ARCHIVE_ROOT);
		
		$notwrittenmessage = true;

		// go through the images and create the thumbnails if required.
		foreach ($images as $image) {

			if (is_null($image[2]) /* Its a folder (or at least not an image) */)
			{
				if ($showFolders)
				{
					$answer[] = $image[1];
				}
			}
			else
			{
				// need to check the size of the thumbnails and resize if appropriate

				if (file_exists($this->THUMBNAIL_ROOT.$image[1]))
				{
					$thumbsize = getimagesize($this->THUMBNAIL_ROOT.$image[1]);
				}
				else
				{
					$thumbsize[0] = 1;
					$thumbsize[1] = 1;
				}

				if ($image[3] /* width */ > $image[4] /* height */)
				{
					$dest_width = $this->MAX_THUMBNAIL_DIMENSION;
					$dest_height = (int)(($this->MAX_THUMBNAIL_DIMENSION * $image[4]) / $image[3]);
				}
				else
				{
					$dest_height = $this->MAX_THUMBNAIL_DIMENSION;
					$dest_width = (int)(($this->MAX_THUMBNAIL_DIMENSION * $image[3]) / $image[4]);
				}

				if (($thumbsize[0] <> $dest_width) && ($thumbsize[1] <> $dest_height) /* not the same size so re-create images */)
				{
					if ($notwrittenmessage) {
						$notwrittenmessage = false;
						$numims = count($images);
						if ($numims > $this->IMAGES_BEFORE_MESSAGE_APPEARS) 
						{
							print "<SCRIPT language=JavaScript>alert('$this->MESSAGE_MANY_THUMBS_TO_CREATE_LEADER$numims$this->MESSAGE_MANY_THUMBS_TO_CREATE_FOLLOWER');</SCRIPT>\n";
							flush();
						}
					}
					// Give the script more time
					set_time_limit($this->TIME_TO_CREATE_ONE_THUMBNAIL);
					// Load and manipulate the image
					$outputImage = $this->LoadImage($image[5].$image[1]);
					// Set the image size
					$outputImage = $this->ResizeImage($outputImage, $dest_width, $dest_height);
					// output the image
					switch ($image[6])
						{
						case 2:
							imagejpeg ($outputImage,$this->THUMBNAIL_ROOT.$image[1],$this->JPEG_QUALITY); 
							break;
						case 3:
							imagepng ($outputImage,$this->THUMBNAIL_ROOT.$image[1]); 
							break;
						}

				}
				
				$answer[][0] = "<IMG border=".$this->IMAGE_LINK_BORDER." src=\"".str_replace(" ","%20",$this->VIRTUAL_THUMBNAIL_ROOT.$image[1])."\" width=$dest_width height=$dest_height>";
				$answer[count($answer)-1][1] = $image[3].$this->SIZE_DELIMITER.$image[4];	// size text in pixels
				$answer[count($answer)-1][2] = $this->VIRTUAL_ARCHIVE_ROOT.$image[1];		// url of image on web site
				$answer[count($answer)-1][3] = $this->ARCHIVE_ROOT.$image[1];					// path to true image on server
				$answer[count($answer)-1][4] = $this->VIRTUAL_THUMBNAIL_ROOT.$image[1];		// path to thumbnail image on web server
				$answer[count($answer)-1][5] = $this->VIRTUAL_ROOT.$image[1];					// path to image on web server
			}

		}

		return $answer;
	}

	// Lists all the images in a directory
	function ListImages($showFolders=true, $ONLYshowFolders=false, $showSize=false, $showPath=false) {

		$this->_CanProceed();

		$answer = "";

		$images = $this->ImagesInDirectory($this->ARCHIVE_ROOT);

		foreach ($images as $image) {
	
			if (is_null($image[2]))
			{  //Its a folder
				if ($showFolders)
				{
					$answer .= $this->LIST_ITEM_LEADER.$image[1].$this->LIST_ITEM_FOLLOWER;
				}
			} 
			elseif ($ONLYshowFolders == false)
			{
				$answer .=  $this->LIST_ITEM_LEADER;
				$answer .=  $image[1];
				if ($showSize)	{ $answer .= ", ".$image[2]; }
				if ($showPath)	{ $answer .= ", ".$image[5]; }
				$answer .=  $this->LIST_ITEM_FOLLOWER;
			}
		}

		return $answer;
	}

	// ===============================================================================
	// ImagesInDirectory - returns the directories/images in the directory as an array
	// with the following values:
	//			$pictures[0] - lower case name (pre-sorted on this key)
	//			$pictures[1] - real case name for display
	//			$pictures[2] - size of image for HTML output (null if a directory)
	//			$pictures[3] - width of image (null if a directory)
	//			$pictures[4] - height of image (null if a directory)
	//			$pictures[5] - directory name
	//			$pictures[6] - image type (null if not an image)
	// ===============================================================================
	function ImagesInDirectory ($dirName) {

		$types = $this->IMAGE_TYPES_TO_DISPLAY;
		$pictures = array();
		$this_regexp = "/$types\z/i"; 
		$counter = 0;
		$dirName = stripslashes($dirName);

		$d = dir($dirName);
		while($entry = $d->read()) {
			if ($entry != "." && $entry != "..") {
				if (is_dir($dirName.$entry)) {
							// Set up the properties of this array item
							$pictures[$counter][0] = strtolower("<!---->".$entry);	// lower case name of directory - comment code makes sure it sorts to the top
							$pictures[$counter][1] = $entry;									// name of directory
							$pictures[$counter][2] = null;
							$pictures[$counter][3] = null;
							$pictures[$counter][4] = null;
							$pictures[$counter][5] = $dirName;								// full path to directory from root
							$pictures[$counter][6] = null;
							$counter++;
				} else {
					if (preg_match($this_regexp, $entry)) {
						//echo $entry."<BR>\n";
						$size = getimagesize ($dirName.$entry);
						if ($size[2] >= 1 && $size[2] <= 3)
						{
							$pictures[$counter][0] = strtolower($entry);		// lower case name of image (for sorting)
							$pictures[$counter][1] = $entry;						// name of image
							$pictures[$counter][2] = $size[3];					// width/height string (might be useful)
							$pictures[$counter][3] = $size[0];					// width in pixels
							$pictures[$counter][4] = $size[1];					// height in pixels
							$pictures[$counter][5] = $dirName;					// full path to directory from root
							$pictures[$counter][6] = $size[2];					// image type
							$counter++;
						}
					}
				}
			}
		}
		$d->close();

		// sort on the first array of the multi-dimensional (not sure how this works, but it seems to do the job!)
		array_multisort ($pictures, SORT_ASC, array_keys ($pictures));

		return $pictures;

	}

	function ImageArchiveScripts()
	{

	$answer = "<script language=\"javascript1.2\">\nfunction submitme(formobj) {\n\tformobj.submit();";
	$answer .= "\n}\n\nfunction sizeandsubmitme(formobj,degs) {\n\tformobj.degrees.value = degs;\n\tformobj.submit();\n";
	$answer .= "}\n</script>\n";

	return $answer;

	}


	function RotateImage($src_img, $degrees = 0) {
		$degrees %= 360;
		if ($degrees == 0) {
				$dst_img = $src_img;
		} Elseif ($degrees == 180) {
				$dst_img = imagerotate($src_img, $degrees, 0);
		} Else {
			$width = imagesx($src_img);
			$height = imagesy($src_img);

			if ($width == $height) {
				$dst_img = imagecreatetruecolor($width, $height);
				$dst_img = imagerotate($src_img, $degrees, 0);
			}
			else
			{
				if ($width > $height) {
						 $size = $width;
				} Else {
						 $size = $height;
				}
						  
				$dst_img = imagecreatetruecolor($size, $size);
				imagecopy($dst_img, $src_img, 0, 0, 0, 0, $width, $height);
				$dst_img = imagerotate($dst_img, $degrees, 0);
				$src_img = $dst_img;
				$dst_img = imagecreatetruecolor($height, $width);
				if ((($degrees == 90) && ($width > $height)) || (($degrees == 270) && ($width < $height))) {
						 imagecopy($dst_img, $src_img, 0, 0, 0, 0, $size, $size);
				}
				if ((($degrees == 270) && ($width > $height)) || (($degrees == 90) && ($width < $height))) {
						imagecopy($dst_img, $src_img, 0, 0, $size - $height, $size - $width, $size, $size);
				}
			}
		}
	return $dst_img;
	}

	// Resizes animage (jpeg image?)
	function ResizeImage($image, $dest_width, $dest_height) {
		$src_img = $image;
		$src_width = imagesx($src_img);
		$src_height = imagesy($src_img);
		if ($dest_height == '') $dest_height  = $src_height;
		if ($dest_width == '') $dest_width  = $src_width;
		$dst_img = imagecreatetruecolor($dest_width,$dest_height);  // creates memroy space for the new image
		imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height);
		imagedestroy($src_img);
		return $dst_img;
		imagedestroy($dst_img);
	}



	function LoadImage ($imgname) { 

		$imgname = stripslashes($imgname);
		$imgtype = getimagesize($imgname);
		switch ($imgtype[2])
		{
		case 2:
			$im = @imagecreatefromjpeg ($imgname); /* Attempt to open */ 
			break;
		case 3:
			$im = @imagecreatefrompng ($imgname); /* Attempt to open */ 
			break;
		default;
			print "<P><B>Not a recognised image type!</B></P>";
			exit;
		}

		if (!$im) { /* See if it failed */ 
			 $im  = imagecreate (150, 30); /* Create a blank image */ 
			 $bgc = imagecolorallocate ($im, 255, 255, 255); 
			 $tc  = imagecolorallocate ($im, 0, 0, 0); 
			 imagefilledrectangle ($im, 0, 0, 150, 30, $bgc); 
			 /* Output an errmsg */ 
			 imagestring ($im, 1, 5, 5, "Error loading $imgname", $tc); 
		} 
		return $im; 
	} 


	// Returns an image on the fly
	function Image_on_Fly($myImage /* PHP Image object */, $myType = 2 /* Image type from getimagesize, can be 3 for png*/) 
	{
		switch ($myType)
		{
		case 3:
			header ("Content-type: image/png"); 
			imagepng ($myImage); 
			break;
		default; /* Default is jpeg */
			header ("Content-type: image/jpeg"); 
			imagejpeg ($myImage,'',$this->JPEG_QUALITY); 
			break;
		}
		ImageDestroy($myImage);
	}


 /* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 
	Private functions

	+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= */

	//checks that all the global variables are set, returns boolean
 	function _CanProceed() {

	$message = "";
	$answer = true;
		

		if (is_null($this->ARCHIVE_ROOT))
		{
			$message .= "<BR>Warning!  \$VIRTUAL_THUMBNAIL_ROOT needs to be set for ImageArchive to work.";
			$answer = false;
		}

		if (is_null($this->THUMBNAIL_ROOT))
		{
			$message .= "<BR>Warning!  \$THUMBNAIL_ROOT needs to be set for ImageArchive to work.";
			$answer = false;
		}

		if (is_null($this->VIRTUAL_ARCHIVE_ROOT))
		{
			$message .= "<BR>Warning!  \$VIRTUAL_ARCHIVE_ROOT needs to be set for ImageArchive to work.";
			$answer = false;
		}

		if (is_null($this->VIRTUAL_THUMBNAIL_ROOT))
		{
			$message .= "<BR>Warning!  \$VIRTUAL_THUMBNAIL_ROOT needs to be set for ImageArchive to work.";
			$answer = false;
		}

		if ($this->ARCHIVE_ROOT == $this->THUMBNAIL_ROOT)
		{
			$message .= "<BR>Warning!  \$ARCHIVE_ROOT and \$THUMBNAIL_ROOT cannot be the same (you lose all your images).";
			$answer = false;
		}

		if ($this->VIRTUAL_ARCHIVE_ROOT == $this->VIRTUAL_THUMBNAIL_ROOT)
		{
			$message .= "<BR>Warning!  \$VIRTUAL_ARCHIVE_ROOT and \$VIRTUAL_THUMBNAIL_ROOT cannot be the same.";
			$answer = false;
		}

		if ($answer != true)
		{
			print $message;
			print "<HR>ImageArchive class not set up correctly.  Exiting...";
			exit;
		}

	}

	function _debug($message)
	{
		if ($this->DEBUGGING)
		{
			print "<DIV style=\"fot-familt: courier new; font-size: 0.7em; color: green; font-weight: bold\">\n";
			print $message;
			print "</DIV>";
		}
	}

	
	function _pageHeader()
	{
		$answer = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
		$answer .= "<HTML>\n<head>\n<link rel=\"STYLESHEET\" type=\"text/css\" href=\"$this->STYLESHEET_HREF\">\n";
		$answer .= "<title>$this->IMAGEARCHIVE_TITLE</title></head>\n";
		return $answer;

	}

	function _pageBodyStart()
	{
		$answer = "<BODY><H1>$this->IMAGEARCHIVE_TITLE</H1>\n";
		return $answer;
	}

	function _pageBodyEnd()
	{
		$answer = "</BODY></HTML>\n";
		return $answer;

	}
}

?>
Return current item: ImageArchive