Location: PHPKode > scripts > phpPhotoGallery > phpphotogallery/phpPhotoGallery.php
<?php

// ********************************************************************************
//
//
//  Author:      				Mammino Claudio
//	Name: 	     				PhpPhotoGallery
// 	Description:   				Thumbnails generator class
//  License:      				GNU General Public License (GPL)
//  Release Date:               11/02/2007
//  Version:                    1.4
//  Contact:					hide@address.com

// For information about this class, see the documentations

// ********************************************************************************

	class phpPhotoGallery
	{
		var $gallery = array();
		var $linkOutput = array();
		var $showErrors;
		var $typeThumbnails;
		var $scaleThumbnails;
		var $typePhoto;
		var $scalePhoto;
		var $fileSupported = array();
		var $shadowEffect;
		var $captionShow;
		
		function phpPhotoGallery ($showErrors, $typeThumbnails = 0, $scaleThumbnails = 150, $typePhoto = 1, $scalePhoto = 100)
		{
			$this->gallery = array();
			$this->linkOutput = array();
			$this->showErrors = $showErrors;
			$this->typeThumbnails = $typeThumbnails;
			$this->scaleThumbnails = $scaleThumbnails;
			$this->typePhoto = $typePhoto;
			$this->scalePhoto = $scalePhoto;
			$this->shadowEffect = true;
			$this->captionShow = true;
			
			$this->fileSupported = array("gif","jpg","jpeg","png");
			$this->writeJavaCode();
		}
		
		function setShadowEffect($val)
		{
			$this->shadowEffect = $val;	
		}
		
		function setShowCaption($val)
		{
			$this->captionShow = $val;
		}
				
		function addDirPhotoNames ($folderName = "", $caption = "No caption specified")
		{
			if(substr($folderName,-1,1) != "/")
				$folderName.= "/";

			if (!is_dir($folderName))
			{
				$this->errorTracker(2, $folderName);
				return;
			}
			
			$dir = dir($folderName);
			
			while ($entry = $dir->read())
			{
				$piecez = explode(".",$entry);
				
				if (in_array(strtolower($piecez[count($piecez) - 1]), $this->fileSupported))
					$this->addImageName($folderName . $entry, $caption);
			}
			
			$dir->close();
		}
		
		function addImageName ($imageName, $caption = "No caption specified", $id = 0)
		{
			if (!is_file($imageName))
			{
				$this->errorTracker(1, $imageName);
				return;
			}
			
			if (list($width, $height, $type) = @getimagesize($imageName))
			{
				if ($id === 0)
					array_push($this->gallery,array($imageName, $width, $height, $type, $caption));
				else
					$this->gallery[$id] =  array($imageName, $width, $height, $type, $caption);
			}
			else
				$this->errorTracker(1, $imageName);
		}
		
		function printSource ()
		{
			echo "<pre>";
			print_r($this->gallery);
			echo "</pre>";
		}
		
		function errorTracker ($errorCode, $wrongText = "")
		{
			if ($this->showErrors)
			{
				switch ($errorCode)
				{
					case 1:
					{
						echo "<b>" . $wrongText . "</b> - Invalid image file.<br>";
						break;
					}
					case 2:
					{
						echo "<b>" . $wrongText . "</b> - Invalid directory.<br>";
						break;
					}
					case 3:
					{
						echo "No images found.<br>";
						break;
					}
				}
			}
		}
		
		function getDimImage ($keyImage, $typeImage, $scaleImage)
		{
			if ($typeImage == 0)
				$newWidth = $scaleImage;
			else if ($typeImage == 1)
				$newWidth = floor(($this->gallery[$keyImage][1] / 100) * $scaleImage);
				
			$newHeight = floor(($newWidth * $this->gallery[$keyImage][2]) / $this->gallery[$keyImage][1]);
			
			$dimImage = array();
			$dimImage[] = $newWidth;
			$dimImage[] = $newHeight;
			
			return ($dimImage);
		}

		function writeJavaCode ()
		{
			print ' <script type="text/javascript" src="onpage.js"></script>

			<script language="javascript1.5" type="text/javascript">
						function openViewPhoto(url,width,height)
						{
							scrollbars = "scrollbars=no";
							
							height_win = height;
							width_win = width;
							
							top_win = (screen.height - height_win) / 2;
							left_win = (screen.width - width_win) / 2;							
							
							if (width > screen.width)
							{
								width_win = screen.width;
								scrollbars = "scrollbars=yes";
								left_win = 0;
							}

							if (height > screen.height)
							{
								height_win = screen.height;
								scrollbars = "scrollbars=yes";
								top_win = 0;
							}
							
							var url_def = "viewImage.php?imagename=" + url + "&task=Image&typeImage=' . $this->typePhoto . '&scaleImage=' . $this->scalePhoto . '";
							var flag = \'width=\' +  width_win + \',height=\' + height_win + \',top=\' + top_win + \',left=\' + left_win + \',toolbar=no,menubar=no,resizable=yes,\' + scrollbars + \',statusbar=no\';
			
							window.open(url_def,\'Pannello\',flag);
						}
					</script>
			';
		}

		function showInTable($photoForRow, $border = 0, $width = 800)
		{
			$i = 0;

			if ($this->shadowEffect)
				echo "<div id=\"minipics\">";
				
			echo "<table cellspacing=\"3\" cellpadding=\"3\" border=\"$border\" width=\"$width\" align=\"center\">";
	
			foreach ($this->linkOutput as $link)
			{
				if ($i <= 0)
					echo "<tr>";
				
				if ($i >= $photoForRow)
				{
					$i = 0;
					echo "</tr>";
				}
		
				$i++;
				echo "<td align=\"center\">". $link . "</td>";
			}

			echo "</tr></table></div>";
		}
		
		function showThumbnail ($keyImage)
		{
			if ($this->captionShow)
				$captionCode = "<br><div style='COLOR: #000000; FONT-FAMILY: Verdana; FONT-SIZE: 8pt;'>" . $this->gallery[$keyImage][4] . "</div>";
			else
				$captionCode = "";

			list($widthPhoto, $heightPhoto) = $this->getDimImage($keyImage, $this->typePhoto, $this->scalePhoto);

			$urlTemp = "viewImage.php?imagename=" . urlencode($this->gallery[$keyImage][0]) . "&task=Thumbnails&typeImage=" . $this->typeThumbnails . "&scaleImage=" . $this->scaleThumbnails;
			$url = "<a href=\"javascript: openViewPhoto('" . $this->gallery[$keyImage][0] . "'," . $widthPhoto . "," . $heightPhoto . ");\" border=\"0\"><img src=\"" . $urlTemp . "\" border=\"0\"></a>" . $captionCode . "\n";
			
			return ($url);
		}
		
		function showGallery ()
		{
			if (count($this->gallery) == 0)
				$this->errorTracker(3);
				
			foreach ($this->gallery as $keyImage => $val) { 
				array_push($this->linkOutput, $this->showThumbnail($keyImage));
			}
		}
	}
?>
Return current item: phpPhotoGallery