Location: PHPKode > projects > Ptk-forensics > ptk/lib/class.tableimagesext.php
<?php

	/**
	 * syntax for the image tag:
	 * 
	 * <C:showimage:<filename> <opt. width> <opt. height>>
	 * 
	 * it is possible to specify the image width without specifying the image height (the image will
	 * be scaled to the appropriate height).
	 * 
	 * supported filename in the image tag:
	 * 
	 * '<C:showimage:'.urlencode('http://myserver.mytld/myimage.png').'>'
	 * '<C:showimage:'.urlencode('/home/my home/my image.png').'>'
	 * 
	 * the url encoding is required for:
	 *  - files from remote servers (first entry from above)
	 *  - local files with whitespaces in the directory or file names
	 * 
	 * local files without whitespaces in their filename can be specified without 
	 * url encoding:
	 * 
	 * '<C:showimage:/home/myhome/myimage.png>'
	 * 
	 * the php gd2 extension must be enabled for remote files and local gif files. 
	 * local png- and jpeg-files are supported without the gd2 extension. 
	 * 
	 * ----
	 * 
	 * required changes in class.ezpdf.php:
	 * 
	 * #4 added include
	 * #27 added new class variable for table extension
	 * #133 initialization of the new class variable
	 * #597-629 new methods
	 * #1066-1067 image parse call added
	 * #1091-1103 image height calculation added
	 * 
	 * @author Kristian Herpel <hide@address.com>
	 * @version 1.11 fix for problems with big pictures (pictures with width > 2000px were not shown in the table)
	 */

	/**
	 * cezpdf extension for displaying images in table cells
	 */
	class CezShowimage {
		
		var $ezPdf = null;
		
		/**
		 * @param Cezpdf $ezpdf current cezpdf object
		 */
		function CezShowimage(& $ezpdf) {
			$this->ezPdf = & $ezpdf;
		}
		
		/**
		 * parses and returns all image tags from the given text 
		 * 
		 * @param String $text input text
		 * @return array found image tags
		 * @access public
		 */
		function getImagesFromText($text = '') {
			preg_match_all("/\<C:showimage:([^>]*)\>/U",$text,$matches);
			return $matches;
		}

		/**
		 * calculate the total height for all images in text
		 * 
		 * @param String $text input text
		 * @return float total height of all images in the text
		 * @access public
		 */ 
		function checkForImage($text) {
			
			$height = 0;
			
			$matches = $this->getImagesFromText($text);
			
			for ($key=0; $key<count($matches[0]); $key++) {
		  	
		  		$params = & CezShowimageParameter::create($matches[1][$key]);
				
				if ($params->getHeight()>0) {
					
					$height = $height + $params->getHeight();
					
				} else {
					
					$height = $height + $params->getOriginalHeight();
					
				}
				
			}
			
			return $height;
			
		}
		
		/**
		 * add a given image to the document
		 * 
		 * @param CezShowimageParameter $params image parameter
		 * @param float $x horizontal position
		 * @param float $y vertical position
		 * @access protected
		 */
		function addImage(& $params, $x = 0, $y = 0) {
			
			if ($params->isUrl()) {
				
				if (function_exists('imagecreatefrompng')) { 
				
					switch($params->getImageType()) {
						
						// png
						case 3: 	$image = imagecreatefrompng($params->getFilename());
									break;
						// jpeg	
						case 2: 	$image = imagecreatefromjpeg($params->getFilename());
									break;
						// gif	
						case 1: 	$image = imagecreatefromgif($params->getFilename());
									break;
					
					}
					
					$this->ezPdf->addImage($image, $x, $y, $params->getWidth(), $params->getHeight());
					
				}
				
			} else {
				
				// check for image type, currently only png and jpeg supported	
				switch($params->getImageType()) {
					
					// png 
					case 3: 	$this->ezPdf->addPngFromFile($params->getFilename(), $x, $y, $params->getWidth(), $params->getHeight());
								break;
					
					// jpeg
					case 2: 	$this->ezPdf->addJpegFromFile($params->getFilename(), $x, $y, $params->getWidth(), $params->getHeight());
								break;
					// gif		
					case 1: 	if (function_exists('imagecreatefromgif')) { 
									$image = imagecreatefromgif($params->getFilename());
									$this->ezPdf->addImage($image, $x, $y, $params->getWidth(), $params->getHeight());
								}
								break;
								
				}
				
			}			
			
		} 
		
		/**
		 * callback method for adding an image to the document
		 * 
		 * note: this method is called by the pdf generator class callback method <code>showImage($info)</code>
		 * 
		 * @param Cezpdf $ezpdf current cezpdf object
		 * @param array $info callback data array (see the callback function part in the R&amp;OS pdf documentation)
		 * @access public 
		 */
		function showimage(& $ezpdf, $info) {

			if ($info['status']=='start') {
				
				$params = & CezShowimageParameter::create($info['p']);
		
				if ($params->isReadable()) {

					$y = ($params->getPositionY()>0) ? $params->getPositionY() : $info['y'];

					$this->addImage($params, $info['x'], $y);

				}
				
			}
		   
		}
		
		/**
		 * searches for <C:showimage:...> and replaces all occurences with extended tag information
		 * 
		 * the extended informations contains the vertical position and the (calculated) image
		 * dimensions
		 * 
		 * @param String $text text to parse
		 * @param int $maxwidth optional maximal width for the image
		 * @param int $maxheight optional maximal height for the image
		 * @param int $currenty current vertical (y) position on the pdf page
		 * @access public
		 */		
		function parseImages(& $text, $maxwidth = 0, $maxheight = 0, $currenty = 0) {
		
			$matches = $this->getImagesFromText($text);
		
			for ($key=0; $key<count($matches[0]); $key++) {
				
				$params = & CezShowimageParameter::create($matches[1][$key]);

				if ($params->isReadable()) {
				
					$width = $params->getWidth();
					$height = $params->getHeight();
					
					if ($width==0 && $height>0) {
			
						$width = $height/$params->getOriginalHeight() * $params->getOriginalWidth();			
						
					} elseif ($height==0 && $width>0) {
						
						$height = $width/$params->getOriginalWidth() * $params->getOriginalHeight();
						
					} elseif ($height==0 && $width==0) {
					
						$width = $params->getOriginalWidth();
						$height = $params->getOriginalHeight();
					
					}
					
					if ($maxwidth>0 && $width>$maxwidth) {
						
						$height = ($maxwidth * $height)/$width;
						$width = $maxwidth;
						
					}
					
					if ($maxheight>0 && $height>$maxheight) {
						
						$width = ($maxheight * $width)/$height;
						$height = $maxheight;
						
					}
					
					$currenty = $currenty - $height;
					
					$imagetag = '<C:showimage:'.$params->getFilename().' '.round($width).' '.round($height).' '.$currenty.'>';
					
				} else {
					
					$imagetag = '';
					
				}
		
				$text = str_replace($matches[0][$key],$imagetag,$text);
				
			}
			
		}
		
		/**
		 * check for images an calculate their maximum width
		 * 
		 * note: the image tags will be removed from the input text, this method is required due to further 
		 * calculation in the original pdf class
		 * 
		 * @param String $text input text 
		 * @return float calculated maximum height
		 * @access public
		 */
		function parseMaximumWidth(& $text) {
			
			$mx = 0;
			
			$matches = $this->getImagesFromText($text);
			
			for ($key=0; $key<count($matches[0]); $key++) {
  	
  				$params = & CezShowimageParameter::create($matches[1][$key]);
			    	
		    	if ($params->getWidth()>$mx) {
		    		
		    		$mx = $params->getWidth();
		    		
		    	} elseif ($params->getOriginalWidth()>$mx) {
		    		
		    		$mx = $params->getOriginalWidth();
		          
		      	}
		      	
		      	// remove the Image-Tag from the text for further calculation
		    	$text = str_replace($matches[0][$key],'',$text);
			      	
		    }
		    
		    $mx = min($mx, $this->ezPdf->ez['pageWidth']);
		    
		    return $mx;
			
		}
		
	}
	
	/**
	 * parameter object 
	 */
	class CezShowimageParameter {
		
		var $filename = '';
		var $width = 0;
		var $height = 0;
		
		var $imageType = 0;
		var $imageHeight = 0;
		var $imageWidth = 0;

		var $IMAGETYPE_GIF = 1;
		var $IMAGETYPE_JPG = 2;
		var $IMAGETYPE_PNG = 3;
		
		var $fileIsUrl = false;
		var $fileIsReadable = false;
		
		var $positionX = 0;
		var $positionY = 0;

		function CezShowimageParameter() {
			
		}
		
		/**
		 * gets the image width
		 * 
		 * @return int image width
		 * @access public
		 */
		function getWidth() {
			return $this->width;
		}
		
		/**
		 * gets the image height 
		 * 
		 * @return int image height
		 * @access public
		 */
		function getHeight() {
			return $this->height;
		}
		
		/**
		 * gets the filename
		 * 
		 * @return String filename
		 * @access public
		 */
		function getFilename() {
			return $this->filename;
		}
		
		function getImagetype() {
			return $this->imageType;
		}
		
		function getOriginalHeight() {
			return $this->imageHeight;
		}
		
		function getOriginalWidth() {
			return $this->imageWidth;
		}
		
		function isUrl() {
			return $this->fileIsUrl;
		}
		
		function isReadable() {
			return $this->fileIsReadable;
		}
		
		function getPositionX() {
			return $this->positionX;
		}
		
		function getPositionY() {
			return $this->positionY;
		}
		
		/**
		 * @access protected
		 */
		function _parse($param = '') {
			
			$params = explode(" ", $param);
			
			$this->filename = urldecode($params[0]);
			
			if (substr($this->filename,0,5)=='http:' || substr($this->filename,0,6)=='https:') {
				$this->fileIsUrl = true;
				$fd = fopen($this->filename, "r");
				$this->fileIsReadable = ($fd!==false) ? true : false;
				fclose($fd); 
			} else {
				$this->fileIsUrl = false;
				$this->fileIsReadable = is_readable($this->filename);
			}
			
			if (isset($params[2]) && $params[2]>0) {
				$this->height = $params[2];	
			}
			
			if (isset($params[1]) && $params[1]>0) {
				$this->width = $params[1];
			}
			
			if (isset($params[3]) && $params[3]>0) {
				$this->positionY = $params[3];
			}
			
			$_imagesize = getImagesize($this->filename);
			
			if (is_array($_imagesize)) {
				
				$this->imageType = $_imagesize[2];
				
				$this->imageWidth = $_imagesize[0]; 
				$this->imageHeight = $_imagesize[1];

			}
			
		}
		
		/**
		 * creates the parameter object from the given parameter list
		 * 
		 * @param String $param parameter list as string
		 * @return CezShowimageParameter parameter object
		 * @access public
		 */
		function & create($param = '') {
			
			$obj = & new CezShowimageParameter();
			$obj->_parse($param);
			return $obj;
			
		}
		
	}

?>
Return current item: Ptk-forensics