Login   Register  
PHP Classes
elePHPant
Icontem

File: clsThumbnailBrowser.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of chris  >  ThumbnailBrowser  >  clsThumbnailBrowser.php  >  Download  
File: clsThumbnailBrowser.php
Role: ???
Content type: text/plain
Description: The source
Class: ThumbnailBrowser
Author: By
Last change:
Date: 2001-10-15 22:57
Size: 6,069 bytes
 

Contents

Class file image Download
<?
/*
************************************************************************
* © Sloppycode.net All rights reserved.
*
* This is a standard copyright header for all source code appearing
* at sloppycode.net. This application/class/script may be redistributed,
* as long as the above copyright remains intact. 
* Comments to sloppycode@sloppycode.net
************************************************************************
*/

/**
 * @title Thumbnail Browser class - simply image thumbnail viewing system.
 * @author C.Small
 * @version 1.0
 */
 
/**
 * DataProducer class
 */
class DataProducer
{
	
		function doDataProducer($startTag,$endTag,$data,$contents)
		{
			return $this->privateDataProducer($startTag,$endTag,$data,$contents);
		}
		function doSingleDataProducer($data,$contents)
		{
			return $this->privateSingleDataProducer($data,$contents);
		}
		function openTemplate($filename)
		{
			return $this->privateopenTemplate($filename);
		}

		function privateDataProducer($startTag,$endTag,$data,$contents)
		{
				// Get start and end points
				$start = strpos($contents,$startTag);
				$end   = strpos($contents,$endTag,$startTag);
				
				// Retrieve everything before start tag
				$prefix = substr($contents,0,$start);
				$prefix = rtrim($prefix);
				
				// Retrieve everything after end tag. Make it starts at the end of end-tag
				$suffix = substr($contents,$end + strlen($endTag),strlen($contents) - ($end + strlen($endTag)));
				//$suffix = ltrim($suffix); <-- source formatting screws up?
				
				// Retrieve data template. make sure it starts at the end of the start-tag.
				$dataTemplate = substr($contents,$start + strlen($startTag),$end - ($start + strlen($startTag)));
				
				// New method implemented here
				for ($i=0; $i <= sizeof($data) -1;$i++)
				{
					$tempReplace = $dataTemplate;
					$tempReplace = rtrim($tempReplace);
					
					$keys = array_keys($data[$i]);
					foreach($keys as $keyname)
					{
						if (!empty($data[$i][$keyname]))
						{
							$tempReplace = str_replace("<".$keyname.">",$data[$i][$keyname],$tempReplace);
						} else{
							$tempReplace = str_replace("<".$keyname.">","",$tempReplace);
						}
					}
					$build .= $tempReplace;
				}
				
				return $prefix . $build . $suffix;
		}
		/**
		 *
		 */
		function privateSingleDataProducer($data,$contents)
		{
			$result = $contents;
			foreach ($data as $tagname => $value){
				$result = str_replace("<".$tagname.">",$value,$result);
			}
			return $result;
		}
		/**
		 *
		 */
		function privateOpenTemplate($filename)
		{		
				$fHnd = @fopen($filename,"r") or die("<br><b>ShowImages class error: Unable to open template: ".$filename."</b>");
				$contents = @fread($fHnd,filesize($filename)) or die("<br><b>Unable to open template: ".$filename."</b>");
				fclose($fHnd);
				
				return $contents;
		}
}
/* -- ----------------------------------------------------------------------------------*/

Class ThumbnailBrowser Extends Dataproducer
{
	// Html templates
	var $template_thumbnails;
	var $template_largeimages;
	
	// Public
	var $rowend_html;
	var $thumbs_per_row;
	var $query_var;
	var $enable_imagesizes = false;
	
	// Private
	var $images = array(); // 2 dimensional array, holding thumnail and large image
	
	function addImage($thumbimage,$largeimage)
	{
		$this->images[] = array($thumbimage,$largeimage);
	}
	
	function privateGetImageInfo($image)
	{
		$result = GetImageSize($image);
		return array($result[0],$result[1]);
	}
	
	function display()
	{
		global $HTTP_GET_VARS;
		
		// Make sure 2 vars are set
		if (empty($this->query_var))
		{
			echo "ThumbnailBrowser class error: query_var variable not set.";
		} elseif (sizeof($this->images) <1) {
			echo "ThumbnailBrowser class error: no images set. See addImage() method to add.";
		} else{
		
			// Display all thumbnails if no get vars
			$largeimageid = $HTTP_GET_VARS[$this->query_var];
			if (empty($largeimageid))
			{
				for ($i=0;$i <= sizeof($this->images) -1;$i++)
				{
					++$n;
					if ($n == $this->thumbs_per_row)
					{
						$tags['rowend_html'] = $this->rowend_html;
						$n = 0;
					} else{
						$tags['rowend_html'] = "";
					}
					if ($this->enable_imagesizes)
					{
						$sizethumb = $this->privateGetImageInfo($this->images[$i][0]);
						$sizelarge = $this->privateGetImageInfo($this->images[$i][1]);
						$tags['thumbimagewidth']  = $sizethumb[0];
						$tags['thumbimageheight'] = $sizethumb[1];
						$tags['largeimagewidth']  = $sizelarge[0];
						$tags['largeimageheight'] = $sizelarge[1];
					} else{
						$tags['thumbimagewidth']  = "";
						$tags['thumbimageheight'] = "";
						$tags['largeimagewidth']  = "";
						$tags['largeimageheight'] = "";
					}
					$tags['imagesrc'] = $this->images[$i][0];
					$tags['imageid']  = $i +1;
					$rows[] = $tags;
					unset($tags);
				}
				$contents = $this->openTemplate($this->template_thumbnails);
				$contents = $this->doDataProducer("<thumbnails>","</thumbnails>",$rows,$contents);
				echo $contents;
			} elseif (!empty($largeimageid)){
				// Display large image
				$tags['largeimage'] = $this->images[$largeimageid -1][1];
				if ($this->enable_imagesizes)
				{
					$sizethumb = $this->privateGetImageInfo($this->images[$largeimageid -1][0]);
					$sizelarge = $this->privateGetImageInfo($this->images[$largeimageid -1][1]);
					$tags['thumbimagewidth']  = $sizethumb[0];
					$tags['thumbimageheight'] = $sizethumb[1];
					$tags['largeimagewidth']  = $sizelarge[0];
					$tags['largeimageheight'] = $sizelarge[1];
				} else{
					$tags['thumbimagewidth']  = "";
					$tags['thumbimageheight'] = "";
					$tags['largeimagewidth']  = "";
					$tags['largeimageheight'] = "";
				}
				$contents = $this->openTemplate($this->template_largeimages);
				$contents = $this->doSingleDataProducer($tags,$contents);
				echo $contents;
			}
		}
		
	}
	
}
?>