class.image.php 24.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
<?php
	/**
	 * this class provide functions to edit an image, e.g. resize, rotate, flip, crop
	 * @author Logan Cai cailongqun [at] yahoo [dot] com [dot] cn
	 * @link  www.phpletter.com
	 * @version 0.9
	 * @since 14/May/2007
	 * @name Image
	 * 
	 */
	
	
	
	
	class Image
	{
		var $_debug = false; 
		var $_errors = array();
		var $gdInfo = array(); //keep all information of GD extension
		var $_imgOrig = null; //the hanlder of original image
		var $_imgFinal = null; //the handler of final image
		var $imageFile  = null;  
    var $transparentColorRed = null;
    var $transparentColorGreen = null;
    var $transparentColorBlue = null;		 
    var $chmod = 0755;
    var $_imgInfoOrig = array(
    	'name'=>'',
    	'ext'=>'',
    	'size'=>'',
    	'width'=>'',
    	'height'=>'',
    	'type'=>'',
    	'path'=>'',
    );    
    var $_imgInfoFinal = array(
    	'name'=>'',
    	'ext'=>'',
    	'size'=>'',
    	'width'=>'',
    	'height'=>'', 
    	'type'=>'',   
    	'path'=>'',
    );		
		var $_imgQuality = 90;
		/**
		 * constructor
		 *
		 * @param boolean $debug
		 * @return Image
		 */
		
		function __construct($debug = false)
		{
			$this->enableDebug($debug);
			$this->gdInfo = $this->getGDInfo();			
		}
		function Image($debug = false)
		{
			$this->__construct($debug);
		}
		/**
		 * enable to debug
		 *
		 * @param boolean $value
		 */
		function enableDebug($value)
		{
			$this->_debug = ($value?true:false);
		}
		/**
		 * check if debug enable
		 * @return boolean
		 */
		function _isDebugEnable()
		{
			return $this->_debug;
		}

	    /**
		 * append to errors array and shown the each error when the debug turned on  
		 * 
		 * @param  string $string
		 * @return void
     * @access private
     * @copyright this function originally come from Andy's php 
	 */
    function _debug($value)
    {
    		$this->_errors[] = $value;
        if ($this->_debug) 
        {
            echo $value . "<br />\n";
        }
    }		
    /**
     * show erros
     *
     */
    function showErrors()
    {
    	if(sizeof($this->_errors))
    	{
    		foreach($this->_errors as $error)
    		{
    			echo $error . "<br />\n";
    		}
    	}
    }
    /**
     * Load an image from the file system.
     * 
     * @param  string $filename
     * @return bool 
     * @access public
     * @copyright this function originally come from Andy's php 
     */
    function loadImage($filename)
    {
        $ext  = strtolower($this->_getExtension($filename));
        $func = 'imagecreatefrom' . ($ext == 'jpg' ? 'jpeg' : $ext);
        if (!$this->_isSupported($filename, $ext, $func, false)) {
            return false;
        }
        if($ext == "gif")
        {
             // the following part gets the transparency color for a gif file
            // this code is from the PHP manual and is written by
            // fred at webblake dot net and webmaster at webnetwizard dotco dotuk, thanks!
            $fp = @fopen($filename, "rb");
            $result = @fread($fp, 13);
            $colorFlag = ord(substr($result,10,1)) >> 7;
            $background = ord(substr($result,11));
            if ($colorFlag) {
                $tableSizeNeeded = ($background + 1) * 3;
                $result = @fread($fp, $tableSizeNeeded);
                $this->transparentColorRed = ord(substr($result, $background * 3, 1));
                $this->transparentColorGreen = ord(substr($result, $background * 3 + 1, 1));
                $this->transparentColorBlue = ord(substr($result, $background * 3 + 2, 1));
            }
            fclose($fp);
            // -- here ends the code related to transparency handling   	
        }
        $this->_imgOrig = @$func($filename);
        if ($this->_imgOrig == null) {
            $this->_debug("The image could not be created from the '$filename' file using the '$func' function.");
            return false;
        }else 
        {
        	$this->imageFile = $filename;
			    $this->_imgInfoOrig = array(
			    	'name'=>basename($filename),
			    	'ext'=>$ext,
			    	'size'=>filesize($filename),
			    	'path'=>$filename,
			    );        	
			    $imgInfo = $this->_getImageInfo($filename);
			    if(sizeof($imgInfo))
			    {
			    	foreach($imgInfo as $k=>$v)
			    	{
			    		$this->_imgInfoOrig[$k] = $v;
			    		$this->_imgInfoFinal[$k] = $v;
			    	}
			    }
			    
        }
        return true;
    }

    /**
     * Load an image from a string (eg. from a database table)
     * 
     * @param  string $string
     * @return bool 
     * @access public
     * @copyright this function originally come from Andy's php 
     */
    function loadImageFromString($string)
    {
    		$this->imageFile = $filename;
        $this->_imgOrig = imagecreatefromstring($string);
        if (!$this->_imgOrig) {
            $this->_debug('The image (supplied as a string) could not be created.');
            return false;
        }
        return true;
    }		
    

    /**
     * Save the modified image
     * 
     * @param  string $filename 
     * @param  int    $quality 
     * @param  string $forcetype 
     * @return bool 
     * @access public
     * @copyright this function originally come from Andy's php 
     */
    function saveImage($filename, $quality = 90, $forcetype = '')
    {
        if ($this->_imgFinal == null) {
            $this->_debug('No changes intend to be made.');
            return false;
        }

        $ext  = ($forcetype == '') ? $this->_getExtension($filename) : strtolower($forcetype);
        $func = 'image' . ($ext == 'jpg' ? 'jpeg' : $ext);
        if (!$this->_isSupported($filename, $ext, $func, true)) 
        {
            return false;
        }
        $saved = false;
        switch($ext) 
        {
            case 'gif':
                if ($this->gdInfo['Truecolor Support'] && imageistruecolor($this->_imgFinal)) 
                {
                    imagetruecolortopalette($this->_imgFinal, false, 255);
                }
            case 'png':
                $saved = $func($this->_imgFinal, $filename);
                break;
            case 'jpg':
                $saved = $func($this->_imgFinal, $filename, $quality);
                break;
        }

        if ($saved === false) 
        {
            $this->_debug("The image could not be saved to the '$filename' file as the file type '$ext' using the '$func' function.");
            return false;
        }else 
        {
        	$this->_imgInfoFinal['size'] = @filesize($filename);
        	@chmod($filename, intval($this->chmod, 8));
        }

        return true;
    }    
    /**
     * Shows the masked image without any saving
     * 
     * @param  string $type 
     * @param  int    $quality 
     * @return bool 
     * @access public
     * @copyright this function originally come from Andy's php 
     */
    function showImage($type = '', $quality = '')
    {
        if ($this->_imgFinal == null) {
            $this->_debug('There is no cropped image to show.');
            return false;
        }
        $type = (!empty($type)?$type:$this->_imgInfoOrig['ext']);
        $quality = (!empty($quality)?$quality:$this->_imgQuality);
				
        $type = strtolower($type);
        $func = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
        $head = 'image/' . ($type == 'jpg' ? 'jpeg' : $type);
        
        if (!$this->_isSupported('[showing file]', $type, $func, false)) {
            return false;
        }

        header("Content-type: $head");
        switch($type) 
        {
            case 'gif':
                if ($this->gdInfo['Truecolor Support'] && imageistruecolor($this->_imgFinal)) 
                {
                    @imagetruecolortopalette($this->_imgFinal, false, 255);
                }
            case 'png':
                $func($this->_imgFinal);
                break;
            case 'jpg':
                $func($this->_imgFinal, '', $quality);
                break;
        }
        return true;
    }    
    
    /**
	 * Used for cropping image
	 * 
	 * @param  int $dst_x
	 * @param  int $dst_y
	 * @param  int $dst_w
	 * @param  int $dst_h
	 * @return bool
     * @access public
     * @copyright this function originally come from Andy's php 
	 */  
    function crop($dst_x, $dst_y, $dst_w, $dst_h)
    {
        if ($this->_imgOrig == null) {
            $this->_debug('The original image has not been loaded.');
            return false;
        }
        if (($dst_w <= 0) || ($dst_h <= 0)) {
            $this->_debug('The image could not be cropped because the size given is not valid.');
            return false;
        }
        if (($dst_w > imagesx($this->_imgOrig)) || ($dst_h > imagesy($this->_imgOrig))) {
            $this->_debug('The image could not be cropped because the size given is larger than the original image.');
            return false;
        }
        $this->_createFinalImageHandler($dst_w, $dst_h);
        if ($this->gdInfo['Truecolor Support']) 
        {
            	if(!@imagecopyresampled($this->_imgFinal, $this->_imgOrig, 0, 0, $dst_x, $dst_y, $dst_w, $dst_h, $dst_w, $dst_h))
            	{
            		$this->_debug('Unable crop the image.');
            		return false;
            	}            
        } else 
        {
          	if(!@imagecopyresized($this->_imgFinal, $this->_imgOrig, 0, 0, $dst_x, $dst_y, $dst_w, $dst_h, $dst_w, $dst_h))
          	{
           		$this->_debug('Unable crop the image.');
          		return false;           		
          	}
            
        }
        $this->_imgInfoFinal['width'] = $dst_w;
        $this->_imgInfoFinal['height'] = $dst_h;   
        return true; 	
    }
  
    
	/**
     * Resize the Image in the X and/or Y direction
     * If either is 0 it will be scaled proportionally
     *
     * @access public
     *
     * @param mixed $new_x 
     * @param mixed $new_y 
     * @param boolean $constraint keep to resize the image proportionally
     * @param boolean $unchangeIfsmaller keep the orignial size if the orignial smaller than the new size
     * 
     *
     * @return mixed none or PEAR_error
     */
	function resize( $new_x, $new_y, $constraint= false, $unchangeIfsmaller=false)
	{
		if(!$this->_imgOrig)
		{
			$this->_debug('No image fould.');
			return false;
		}		
		
		$new_x = intval($new_x);
		$new_y = intval($new_y);
		if($new_x <=0 || $new_y <= 0)
		{
			$this->_debug('either of new width or height can be zeor or less.');
		}else 
		{
		
			if($constraint)
			{
			

	      if($new_x > 0 && $new_y > 0)
	      {
	      	$ratio = min($new_x/$this->_imgInfoOrig['width'], $new_y / $this->_imgInfoOrig['height']);
	      }elseif($new_x > 0)
	      {
	      	$ratio = $new_x / $this->_imgInfoOrig['width'];
	      }elseif($new_y > 0)
	      {
	      	$ratio = $new_y / $this->_imgInfoOrig['height'];
	      }else 
	      {
	      	$ratio = 1;
	      }

	      $ratio = ($ratio > 1?1:$ratio);	 		
		    $new_x = $ratio * $this->_imgInfoOrig['width'];
		    $new_y = $ratio * $this->_imgInfoOrig['height'];					
					
			}

		
			
			
			if(is_null($this->_imgOrig))
			{
				$this->loadImage($filePath);
			}
			if(sizeof($this->_errors) == 0)
			{
				return $this->_resize($new_x, $new_y);
			}			
		}

		return false;
		
	} // End resize    
 	/**
     * resize the image and return the thumbnail image  details array("width"=>, "height"=>, "name")
     *
     * @param string $fileName 
     * @param int $new_x the thumbnail width
     * @param int $new_y the thumbnail height
     * @param string $mode can be save, view and both
     * @return unknown
     */
	function _resize( $new_x, $new_y) 
	{
		$this->_createFinalImageHandler($new_x, $new_y);
    // hacks fot transparency of png24 files
    if ($this->_imgInfoOrig['type'] == 'png') 
    {    
        @imagealphablending($this->_imgFinal, false);
				if(function_exists('ImageCopyResampled'))
				{
					@ImageCopyResampled($this->_imgFinal, $this->_imgOrig, 0, 0, 0, 0, $new_x, $new_y, $this->_imgInfoOrig['width'], $this->_imgInfoOrig['height']);
				} else {
					@ImageCopyResized($this->_imgFinal, $this->_imgOrig, 0, 0, 0, 0, $new_x, $new_y, $this->_imgInfoOrig['width'], $this->_imgInfoOrig['height']);
				} 
        @imagesavealpha($this->_imgFinal, true);

    }else 
    {//for the rest image
			if(function_exists('ImageCopyResampled'))
			{
				@ImageCopyResampled($this->_imgFinal, $this->_imgOrig, 0, 0, 0, 0, $new_x, $new_y, $this->_imgInfoOrig['width'], $this->_imgInfoOrig['height']);
			} else {
				@ImageCopyResized($this->_imgFinal, $this->_imgOrig, 0, 0, 0, 0, $new_x, $new_y, $this->_imgInfoOrig['width'], $this->_imgInfoOrig['height']);
			}    	
    }

		
		$this->_imgInfoFinal['width'] = $new_x;
		$this->_imgInfoFinal['height'] = $new_y;
		$this->_imgInfoFinal['name'] = basename($this->_imgInfoOrig['name']);
		$this->_imgInfoFinal['path'] = $this->_imgInfoOrig['path'];		
		if($this->_imgFinal)
		{
			return true;
		}else 
		{			
			$this->_debug('Unable to resize the image on the fly.');
			return false;
							
		}

	}   
    /**
	 * Get the extension of a file name
	 * 
	 * @param  string $file
 	 * @return string
     * @copyright this function originally come from Andy's php 
	 */
    function _getExtension($file)
    {
        $ext = '';
        if (strrpos($file, '.')) {
            $ext = strtolower(substr($file, (strrpos($file, '.') ? strrpos($file, '.') + 1 : strlen($file)), strlen($file)));
        }
        return $ext;
    }

	    /**
		 * Validate whether image reading/writing routines are valid.
		 * 
		 * @param  string $filename
		 * @param  string $extension
		 * @param  string $function
		 * @param  bool   $write
		 * @return bool
     * @access private
     * @copyright this function originally come from Andy's php 
	 */
    function _isSupported($filename, $extension, $function, $write = false)
    {

       $giftype = ($write) ? ' Create Support' : ' Read Support';
        $support = strtoupper($extension) . ($extension == 'gif' ? $giftype : ' Support');

        if (!isset($this->gdInfo[$support]) || $this->gdInfo[$support] == false) {
            $request = ($write) ? 'saving' : 'reading';
            $this->_debug("Support for $request the file type '$extension' cannot be found.");
            return false;
        }
        if (!function_exists($function)) {
            $request = ($write) ? 'save' : 'read';
            $this->_debug("The '$function' function required to $request the '$filename' file cannot be found.");
            return false;
        }

        return true;
    }
    /**
     * flip image horizotally or vertically
     *
     * @param string $direction
     * @return boolean
     */
    function flip($direction="horizontal")
    {
				$this->_createFinalImageHandler($this->_imgInfoOrig['width'], $this->_imgInfoOrig['height']);
			if($direction != "vertical")
			{
				$dst_x = 0;
				$dst_y = 0;
				$src_x = $this->_imgInfoOrig['width'] -1;
				$src_y = 0;
				$dst_w = $this->_imgInfoOrig['width'];
				$dst_h = $this->_imgInfoOrig['height'];
				$src_w = 0 - $this->_imgInfoOrig['width'];
				$src_h = $this->_imgInfoOrig['height'];
				
			}else 
			{
				$dst_x = 0;
				$dst_y = 0;
				$src_x = 0;
				$src_y = $this->_imgInfoOrig['height'] - 1;
				$dst_w = $this->_imgInfoOrig['width'];
				$dst_h = $this->_imgInfoOrig['height'];
				$src_w = $this->_imgInfoOrig['width'];
				$src_h = 0 - $this->_imgInfoOrig['height'];				
			}			
				if(function_exists('ImageCopyResampled')){
					ImageCopyResampled($this->_imgFinal, $this->_imgOrig, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
				} else {
					ImageCopyResized($this->_imgFinal, $this->_imgOrig, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
				}
				$this->_imgInfoFinal['width'] = $dst_w;
				$this->_imgInfoFinal['height'] = $dst_h;
				$this->_imgInfoFinal['name'] = basename($this->imageFile);
				$this->_imgInfoFinal['path'] = $this->imageFile;		
				if($this->_imgFinal)
				{
					return true;
				}else 
				{			
					$this->_debug('Unable to resize the image on the fly.');	
					return false;
								
				}   	
    }
    /**
     * flip vertically
     *
     * @return boolean
     */
    function flipVertical()
    {
    	return $this->flip('vertical');
    }
    /**
     * flip horizontal
     *
     * @return string
     */
    function flipHorizontal()
    {
    	return $this->flip('horizontal');
    }


    /**
     * get the GD version information
     *
     * @param  bool $versionOnly
     * @return array
     * @access private
     * @copyright this function originally come from Andy's php 
     */
    function getGDInfo($versionOnly = false)
    {
        $outputs = array();
        if (function_exists('gd_info')) 
        {
            $outputs = gd_info();
        } else 
        {
            $gd = array(
                    'GD Version'         => '',
                    'GIF Read Support'   => false,
                    'GIF Create Support' => false,
                    'JPG Support'        => false,
                    'PNG Support'        => false,
                    'FreeType Support'   => false,
                    'FreeType Linkage'   => '',
                    'T1Lib Support'      => false,
                    'WBMP Support'       => false,
                    'XBM Support'        => false       
                    );
            ob_start();
            phpinfo();
            $buffer = ob_get_contents();
            ob_end_clean();
            foreach (explode("\n", $buffer) as $line) {
                $line = array_map('trim', (explode('|', strip_tags(str_replace('</td>', '|', $line)))));
                if (isset($gd[$line[0]])) {
                    if (strtolower($line[1]) == 'enabled') {
                        $gd[$line[0]] = true;
                    } else {
                        $gd[$line[0]] = $line[1];
                    }
                }
            }
            $outputs = $gd;
        }

        if (isset($outputs['JIS-mapped Japanese Font Support'])) {
            unset($outputs['JIS-mapped Japanese Font Support']);
        }
        if (function_exists('imagecreatefromgd')) {
            $outputs['GD Support'] = true;
        }
        if (function_exists('imagecreatefromgd2')) {
            $outputs['GD2 Support'] = true;
        }
        if (preg_match('/^(bundled|2)/', $outputs['GD Version'])) {
            $outputs['Truecolor Support'] = true;
        } else {
            $outputs['Truecolor Support'] = false;
        }
        if ($outputs['GD Version'] != '') {
            $match = array();
            if (preg_match('/([0-9\.]+)/', $outputs['GD Version'], $match)) {
                $foo = explode('.', $match[0]);
                $outputs['Version'] = array('major' => isset($foo[0])?$foo[0]:'', 'minor' => isset($foo[1])?$foo[1]:'', 'patch' => isset($foo[2])?$foo:"");
            }
        }

        return ($versionOnly) ? $outputs['Version'] : $outputs;
    }    
    
    /**
	 * Destroy the resources used by the images.
	 * 
	 * @param  bool $original
	 * @return void
     * @access public
     * @copyright this function originally come from Andy's php 
	 */
    function DestroyImages($original = true)
    {
    		if(!is_null($this->_imgFinal))
    		{
    			@imagedestroy($this->_imgFinal);
    		}        
        $this->_imgFinal = null;
        if ($original && !is_null($this->_imgOrig)) {
            @imagedestroy($this->_imgOrig);
            $this->_imgOrig = null;
        }
    } 
    
	function getImageInfo($imagePath)
	{
		return $this->_getImageInfo($imagePath);
	}
	/**
     * get image information, e.g. width, height, type
     * @access public
     * @return array
     */
	function _getImageInfo($imagePath)
	{
		$outputs = array();
		$imageInfo = @GetImageSize($imagePath);
		if ($imageInfo && is_array($imageInfo))
		{
			switch($imageInfo[2]){
				case 1:
					$type = 'gif';
					break;
				case 2:
					$type = 'jpeg';
					break;
				case 3:
					$type = 'png';
					break;
				case 4:
					$type = 'swf';
					break;
				case 5:
					$type = 'psd';
				case 6:
					$type = 'bmp';
				case 7:
				case 8:
					$type = 'tiff';
				default:
					$type = '';
			}
			$outputs['width'] = $imageInfo[0];
			$outputs['height'] = $imageInfo[1];
			$outputs['type'] = $type;
			$outputs['ext'] = $this->_getExtension($imagePath);
		} else {
			$this->_debug('Unable locate the image or read images information.');
		}
		return $outputs;
		
	}
	  function rotate($angle, $bgColor=0)
    {
    	$angle = intval($angle) -360;
    		while($angle <0)
    		{
    			$angle += 360;
    		}
        if(!function_exists("imagerotate"))
        {
            function imagerotate($src_img, $angle)
            {
                $src_x = @imagesx($src_img);
                $src_y = @imagesy($src_img);
                if ($angle == 180)
                {
                    $dest_x = $src_x;
                    $dest_y = $src_y;
                }
                elseif ($src_x <= $src_y)
                {
                    $dest_x = $src_y;
                    $dest_y = $src_x;
                }
                elseif ($src_x >= $src_y) 
                {
                    $dest_x = $src_y;
                    $dest_y = $src_x;
                }        
		 		if(function_exists('ImageCreateTrueColor'))
		 		{
					$rotate = @ImageCreateTrueColor($dest_x,$dest_y);
				} else 
				{
					$rotate = @ImageCreate($dest_x,$dest_y);
				} 
                @imagealphablending($rotate, false);
               
                switch ($angle)
                {
                    case 270:
                        for ($y = 0; $y < ($src_y); $y++)
                        {
                            for ($x = 0; $x < ($src_x); $x++)
                            {
                                $color = imagecolorat($src_img, $x, $y);
                                imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
                            }
                        }
                        break;
                    case 90:
                        for ($y = 0; $y < ($src_y); $y++)
                        {
                            for ($x = 0; $x < ($src_x); $x++)
                            {
                                $color = imagecolorat($src_img, $x, $y);
                                imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
                            }
                        }
                        break;
                    case 180:
                        for ($y = 0; $y < ($src_y); $y++)
                        {
                            for ($x = 0; $x < ($src_x); $x++)
                            {
                                $color = imagecolorat($src_img, $x, $y);
                                imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
                            }
                        }
                        break;
                    default: $rotate = $src_img;
                };
                return $rotate;
            }
        }

         if($this->_imgFinal = @imagerotate($this->_imgOrig, $angle, $bgColor))
         {
         	return true;
         }else 
         {
         	return false;
         }
 
       
    }
	/**
	 * get the original image info
	 *
	 * @return array
	 */
	function getOriginalImageInfo()
	{
		return $this->_imgInfoOrig;
	}
	/**
	 * return the final image info
	 *
	 * @return array
	 */
	function getFinalImageInfo()
	{
		if($this->_imgInfoFinal['width'] == '')
		{
			if(is_null($this->_imgFinal))
			{
				$this->_imgInfoFinal = $this->_imgInfoOrig;
			}else 
			{
				$this->_imgInfoFinal['width'] = @imagesx($this->_imgFinal);
				$this->_imgInfoFinal['height'] = @imagesy($this->_imgFinal);
			}
		}
		return $this->_imgInfoFinal;
	}
	
    /**
     *  create final image handler
     *
     *  @access private
     *  @param $dst_w width
     * 	@param $dst_h height
     * 	@return boolean
     * 	@copyright original from noname at nivelzero dot ro
     */
    function _createFinalImageHandler($dst_w, $dst_h)
    {
		 		if(function_exists('ImageCreateTrueColor'))
		 		{
					$this->_imgFinal = @ImageCreateTrueColor($dst_w,$dst_h);
				} else {
					$this->_imgFinal = @ImageCreate($dst_w,$dst_h);
				}   
        if (!is_null($this->transparentColorRed) && !is_null($this->transparentColorGreen) && !is_null($this->transparentColorBlue)) {
        
            $transparent = @imagecolorallocate($targetImageIdentifier, $this->transparentColorRed, $this->transparentColorGreen, $this->transparentColorBlue);
            @imagefilledrectangle($this->_imgFinal, 0, 0, $dst_w, $dst_h, $transparent);
            @imagecolortransparent($this->_imgFinal, $transparent);            
        }
        
    }	
	}
	
?>