Version: image.class 1.2

Type: Class

Category: File Management

License: GNU General Public License

Description: * Script : Image resize and croping class. * You can Write copyright text and attach your logo write on image with this class * * ***************************************************************************************** * * * $obj = new pg_image($arg); * * * ----------------------------------------------------------------------------------------- * Varibles Information * [0] type => (POST | FILE) => Source File Send Type _FILES OR Dir * [1] file => ({if POST Array file = $_FILES['yourfile']}|{if FILE false}) => Source File filesource * [2] path => ({if FILE String [ dirname/filename.extension ]}) => Source File Root Path * [3] target => ({if FILE String [ dirname/filename.extension ]}) => Target File Root Path * [4] width => (integer) => Target File Width * [5] height => (integer) => Target File Height * [6] quality => (integer 1 - 100) => Target File Quality 0-100 % * [7] action => (crop | resize) => Target Action "resize" OR "crop" * [8] bordersize => (integer 0-5) => Target Border Size * [9] bordercolor => (color hex) => Target Border Color * [10] bgcolor => (color hex) => Target Background Color * [11] copytext => (String) => Copyright Content Text * [12] copycolor => (color hex) => Copyright Text Color * [13] copyshadow => (color hex) => Copyright Text Shadow color * [14] copybgcolor => (color hex) => Copyright Background Color * [15] copybgshadow => (color hex) => Copyright Background Shadow Color * [16] copybordersize => (integer 0-5) => Copyright Border Size 1-3 * [17] copybordercolor => (color hex) => Copyright Border Color * [18] copyposition => (top | topright | topleft | center | left | right | bottom | bottomright | bottomleft) => Copyright Position * [19] logoposition => (top | topright | topleft | center | left | right | bottom | bottomright | bottomleft) => Logo Image Position * [20] logoimage => (String [ dirname/filename.extension] Allow Extension (PNG | GIF | JPG)) => Logo Image Root Path "PNG" OR "GIF" OR "JPG"



<?php

	/**
	 * *****************************************************************************************
	 * Author 		: Halit YESIL
	 * www			: www.halityesil.com, www.prografik.net, www.trteknik.net
	 * email		: mail@halityesil.com, halit.yesil@prografik.net, halit.yesil@trteknik.net
	 * GSM			: +90.535 648 3504
	 * Create Date	: 21 / 03 / 2007
	 * 
	 * 
	 * Script		: Image resize and croping class. 
	 * 				 You can Write copyright text and attach your logo write on image with this class
	 * 
	 * *****************************************************************************************
	 * 
	 * 
	 * $obj = new pg_image($arg);
	 *
	 * 
	 * -----------------------------------------------------------------------------------------
	 * Varibles Information
	 * 		[0] type => (POST | FILE) => Source File Send Type _FILES OR Dir
	 * 		[1] file => ({if POST Array file = $_FILES['yourfile']}|{if FILE false}) => Source File filesource
	 * 		[2] path => ({if FILE String [ dirname/filename.extension ]}) => Source File Root Path
	 * 		[3] target => ({if FILE String [ dirname/filename.extension ]}) => Target File Root Path
	 * 		[4] width => (integer) => Target File Width
	 * 		[5] height => (integer) => Target File Height
	 * 		[6] quality => (integer 1 - 100) => Target File Quality 0-100 %
	 * 		[7] action => (crop | resize) => Target Action "resize" OR "crop"
	 * 		[8] bordersize => (integer 0-5) => Target Border Size
	 * 		[9] bordercolor => (color hex) => Target Border Color
	 * 		[10] bgcolor => (color hex) => Target Background Color
	 * 		[11] copytext => (String) => Copyright Content Text
	 * 		[12] copycolor => (color hex) => Copyright Text Color
	 * 		[13] copyshadow => (color hex) => Copyright Text Shadow color
	 * 		[14] copybgcolor => (color hex) => Copyright Background Color
	 * 		[15] copybgshadow => (color hex) => Copyright Background Shadow Color
	 * 		[16] copybordersize => (integer 0-5) => Copyright Border Size 1-3
	 * 		[17] copybordercolor => (color hex) => Copyright Border Color
	 *  	[18] copyposition => (top | topright | topleft | center | left | right | bottom | bottomright | bottomleft) => Copyright Position
	 * 		[19] logoposition => (top | topright | topleft | center | left | right | bottom | bottomright | bottomleft) => Logo Image Position
	 * 		[20] logoimage => (String [ dirname/filename.extension] Allow Extension (PNG | GIF | JPG)) => Logo Image Root Path "PNG" OR "GIF" OR "JPG"
	 * 
	 * 
	 * 
	 * 
	 * -----------------------------------------------------------------------------------------
	 * $arg => string or array
	 * -----------------------------------------------------------------------------------------
	 * is Array keystring
	 * 		type => (POST | FILE)
	 * 		file => ({if POST Array file = $_FILES['yourfile']}|{if FILE false})
	 * 		path => ({if FILE String [ dirname/filename.extension ]})
	 * 		target => ({if FILE String [ dirname/filename.extension ]}) => Target File Root Path
	 * 		width => (integer)
	 * 		height => (integer)
	 * 		quality => (integer 1 - 100)
	 * 		action => (crop | resize)
	 * 		bordersize => (integer 0-5)
	 * 		bordercolor => (color hex)
	 * 		bgcolor => (color hex)
	 * 		copytext => (String)
	 * 		copycolor => (color hex)
	 * 		copyshadow => (color hex)
	 * 		copybgcolor => (color hex)
	 * 		copybgshadow => (color hex)
	 * 		copybordersize => (integer 0-5)
	 * 		copybordercolor => (color hex)
	 *  	copyposition => (top | topright | topleft | center | centerleft | centerright | bottom | bottomright | bottomleft)
	 * 		logoposition => (top | topright | topleft | center | centerleft | centerright | bottom | bottomright | bottomleft)
	 * 		logoimage => (String [ dirname/filename.extension] Allow Extension (PNG | GIF | JPG))
	 *
	 * -----------------------------------------------------------------------------------------
	 * is Array key integer
	 * 		[0] type => (POST | FILE)
	 * 		[1] file => ({if POST Array file = $_FILES['yourfile']}|{if FILE false})
	 * 		[2] path => ({if FILE String [ dirname/filename.extension ]})
	 * 		[3] target => ({if FILE String [ dirname/filename.extension ]}) => Target File Root Path
	 * 		[4] width => (integer)
	 * 		[5] height => (integer)
	 * 		[6] quality => (integer 1 - 100)
	 * 		[7] action => (crop | resize)
	 * 		[8] bordersize => (integer 0-5)
	 * 		[9] bordercolor => (color hex)
	 * 		[10] bgcolor => (color hex)
	 * 		[11] copytext => (String)
	 * 		[12] copycolor => (color hex)
	 * 		[13] copyshadow => (color hex)
	 * 		[14] copybgcolor => (color hex)
	 * 		[15] copybgshadow => (color hex)
	 * 		[16] copybordersize => (integer 0-5)
	 * 		[17] copybordercolor => (color hex)
	 *  	[18] copyposition => (top | topright | topleft | center | left | right | bottom | bottomright | bottomleft)
	 * 		[19] logoposition => (top | topright | topleft | center | left | right | bottom | bottomright | bottomleft)
	 * 		[20] logoimage => (String [ dirname/filename.extension] Allow Extension (PNG | GIF | JPG))
	 * 
	 * 
	 * -----------------------------------------------------------------------------------------
	 * is String
	 * 	STRING DECLERATIONS 
	 * 		"path,target,width,height,quality,action" 
	 * 	OR 
	 * 		"path=apthvalue&target=value&width=int&height=int&quality=int&action=(crop|resize)"
	 * 		
	 * 		Example 1
	 * 			$obj = new pg_image("source/source.jpg,newimage/resource.png,150,100,80,crop");
	 * 		
	 * 		Example 2
	 * 			$obj = new pg_image("path=source/source.jpg&target=newimage/resource.png&width=150&height=100&quality=80&action=crop");
	 * 
	 * 		[0] path => ({if FILE String [ dirname/filename.extension ]})
	 * 		[1] target => ({if FILE String [ dirname/filename.extension ]}) => Target File Root Path
	 * 		[2] width => (integer)
	 * 		[3] height => (integer)
	 * 		[4] quality => (integer 1 - 100)
	 * 		[5] action => (crop | resize)
	 * 
	 * 
	 * -----------------------------------------------------------------------------------------
	 * Position Options
	 * 		
	 * 		top
	 * 		topright
	 * 		topleft
	 * 		center
	 * 		left
	 * 		right
	 * 		bottom
	 * 		bottomright
	 * 		bottomleft
	 * 
	 * 		+------------------------------------------+
	 * 		|topleft------------top------------topright|
	 * 		|------------------------------------------|
	 * 		|------------------------------------------|
	 * 		|left--------------center-------------right|
	 * 		|------------------------------------------|
	 * 		|------------------------------------------|
	 * 		|bottomleft--------bottom-------bottomright|
	 * 		+------------------------------------------+
	 * 
	 * 
	 * -----------------------------------------------------------------------------------------
	 * Crop Option (Example)
	 * 		
	 * 		Orginal image width = 200px, height = 150px
	 * 		+--------------+
	 * 		|**************|
	 * 		|**************|
	 * 		|**************|
	 * 		|**************|
	 * 		+--------------+
	 * 
	 * 		Crop Size width = 150px, height = 100px
	 * 		+---------+
	 * 		|*********|
	 * 		|*********|
	 * 		|*********|
	 * 		+---------+
	 * 		
	 * 		Output size width = 150px, height = 100px
	 * 		+---------+
	 * 		|*********|
	 * 		|*********|
	 * 		|*********|
	 * 		+---------+
	 * 
	 * -----------------------------------------------------------------------------------------
	 * Resize Option (Example)
	 * 		
	 * 		Orginal image width = 200px, height = 150px
	 * 		+--------------+
	 * 		|**************|
	 * 		|**************|
	 * 		|**************|
	 * 		|**************|
	 * 		+--------------+
	 * 
	 * 		Resize Size width = 150px, height = 100px
	 * 		+---------+
	 * 		|*********|
	 * 		|*********|
	 * 		|*********|
	 * 		+---------+
	 * 		
	 * 		Output size width = 133px, height = 100px
	 * 		+-------+
	 * 		|*******|
	 * 		|*******|
	 * 		|*******|
	 * 		+-------+
	 * 
	 */
	class pg_image
	{
		
		var $var = false;
		var $arg;
		
		var $imgobj;
		var $fontsize = 8;
		var $copyfont = 'Arial';
		var $padding_top = 5;
		var $padding_bottom = 5;
		var $padding_left = 5;
		var $padding_right = 5;
		var $imgtip = 'true';
		
		/**
		 * Class Object Start Function
		 *
		 * @param array or string $arg
		 * @return pg_image
		 */
		function pg_image($arg){
			if(!$arg || $arg == '') return false;
			$this->arg = $arg;
			
			if(is_array($arg))
				$prarg = $this->ParseArgArray();
			else 
				$prarg = $this->ParseArgString();
			
			if(is_array($prarg))
				$this->var = $prarg;
			else
				return false;
				
			$acout = $this->startAction();
			if(!$acout)	
				return false;
			else 
				return true;
		}
		
		/**
		 * String Argument Parser
		 *
		 * @return Array
		 */
		function ParseArgString(){
			$a = $this->arg;
			if(!$a || $a == '') return false;
			if(is_array($a)) return false;
			
			if(eregi(",",$a)){
				$r = @ explode(",",$a,8);
				if(!is_array($r)) return false;
				$c = count($r);
				if($c != 6) return false;
				
				$filename = getimagesize($r[0]);
				$ow = $filename[0];
				$oh = $filename[1];
				$omim = $filename[6];
				$osize = '';
				$oname = $this->filename($r[0], 'filename');
				$oex = $this->filename($r[0], 'extension');
				$opath = $r[0];
								
				$var['source']['type'] = 'FILE';
				$var['source']['width'] = $ow;
				$var['source']['height'] = $oh;
				$var['source']['size'] = $osize;
				$var['source']['path'] = $opath;
				$var['source']['name'] = $oname;
				$var['source']['ex'] = $oex;
				
				$var['resource']['width'] = $r[2];
				$var['resource']['height'] = $r[3];
				$var['resource']['quality'] = $r[4];
				$var['resource']['path'] = $r[1];
				$var['resource']['name'] = $this->filename($var['resource']['path'], 'filename');
				$var['resource']['ex'] = $this->filename($var['resource']['path'], 'extension');
				$var['resource']['action'] = ($r[5] == 'crop')?'crop':'resize';
				
				return $var;

			}elseif (eregi("&",$a)){
				
				parse_str($a, $r);
				if(!is_array($r)) return false;
			
				$filename = getimagesize($r['path']);
				$ow = $filename[0];
				$oh = $filename[1];
				$omim = $filename[6];
				$osize = '';
				$oname = $this->filename($r['path'], 'filename');
				$oex = $this->filename($r['path'], 'extension');
				$opath = $r['path'];
								
				$var['source']['type'] = 'FILE';
				$var['source']['width'] = $ow;
				$var['source']['height'] = $oh;
				$var['source']['size'] = $osize;
				$var['source']['path'] = $opath;
				$var['source']['name'] = $oname;
				$var['source']['ex'] = $oex;
				
				$var['resource']['width'] = $r['width'];
				$var['resource']['height'] = $r['height'];
				$var['resource']['quality'] = $r['quality'];
				$var['resource']['path'] = $r['target'];
				$var['resource']['name'] = $this->filename($var['resource']['path'], 'filename');
				$var['resource']['ex'] = $this->filename($var['resource']['path'], 'extension');
				$var['resource']['action'] = ($r['action'] == 'crop')?'crop':'resize';
				return $var;
			}else return false;
		}

		/**
		 * Array Arguments Parser
		 *
		 * @return Array
		 */
		function ParseArgArray(){
			$a = $this->arg;
			if(!is_array($a)) return false;
			
			if(array_key_exists('file', $a)){
				
				if($a['type'] == 'POST'){
					$filename = getimagesize($a['file']['tmp_name']);
					$ow = $filename[0];
					$oh = $filename[1];
					$omim = $filename[6];
					$osize = $a['file']['size'];
					$oname = $this->filename($a['file']['name'], 'filename');
					$oex = $this->filename($a['file']['name'], 'extension');
					$opath = $a['file']['tmp_name'];
				}else{
					$filename = getimagesize($a['path']);
					$ow = $filename[0];
					$oh = $filename[1];
					$omim = $filename[6];
					$osize = $a['file']['size'];
					$oname = $this->filename($a['path'], 'filename');
					$oex = $this->filename($a['path'], 'extension');
					$opath = $a['path'];
				}
								
				$var['source']['type'] = $a['type'];
				$var['source']['width'] = $ow;
				$var['source']['height'] = $oh;
				$var['source']['size'] = $osize;
				$var['source']['path'] = $opath;
				$var['source']['file'] = $a['file'];
				$var['source']['name'] = $oname;
				$var['source']['ex'] = $oex;
				
				$var['resource']['width'] = $a['width'];
				$var['resource']['height'] = $a['height'];
				$var['resource']['quality'] = $a['quality'];
				$var['resource']['path'] = $a['target'];
				$var['resource']['name'] = $this->filename($var['resource']['path'], 'filename');
				$var['resource']['ex'] = $this->filename($var['resource']['path'], 'extension');
				$var['resource']['action'] = ($a['action'] == 'crop')?'crop':'resize';
				$var['resource']['bordersize'] = $a['bordersize'];
				$var['resource']['bordercolor'] = $a['bordercolor'];
				$var['resource']['bgcolor'] = $a['bgcolor'];
				
				$var['copyright']['text'] = $a['copytext'];
				$var['copyright']['color'] = $a['copycolor'];
				$var['copyright']['shadow'] = $a['copyshadow'];
				$var['copyright']['bgcolor'] = $a['copybgcolor'];
				$var['copyright']['bgshadow'] = $a['copybgshadow'];
				$var['copyright']['bordersize'] = $a['copybordersize'];
				$var['copyright']['bordercolor'] = $a['copybordercolor'];
				$var['copyright']['position'] = $a['copyposition'];
				
				$var['logo']['position'] = $a['logoposition'];
				$var['logo']['image'] = $a['logoimage'];
				
				return $var;
			}elseif (array_key_exists(0, $a)){
				
				if($a[0] == 'POST'){
					$filename = getimagesize($a[1]['tmp_name']);
					$ow = $filename[0];
					$oh = $filename[1];
					$omim = $filename[6];
					$osize = $a[1]['size'];
					$oname = $this->filename($a[1]['name'], 'filename');
					$oex = $this->filename($a[1]['name'], 'extension');
					$opath = $a[1]['tmp_name'];
				}else{
					$filename = getimagesize($a[2]);
					$ow = $filename[0];
					$oh = $filename[1];
					$omim = $filename[6];
					$osize = $a[1]['size'];
					$oname = $this->filename($a[2], 'filename');
					$oex = $this->filename($a[2], 'extension');
					$opath = $a[2];
				}
								
				$var['source']['type'] = $a[0];
				$var['source']['width'] = $ow;
				$var['source']['height'] = $oh;
				$var['source']['size'] = $osize;
				$var['source']['path'] = $opath;
				$var['source']['file'] = $a[1];
				$var['source']['name'] = $oname;
				$var['source']['ex'] = $oex;
				
				$var['resource']['width'] = $a[4];
				$var['resource']['height'] = $a[5];
				$var['resource']['quality'] = $a[6];
				$var['resource']['path'] = $a[3];
				$var['resource']['name'] = $this->filename($var['resource']['path'], 'filename');
				$var['resource']['ex'] = $this->filename($var['resource']['path'], 'extension');
				$var['resource']['action'] = ($a[7] == 'crop')?'crop':'resize';
				$var['resource']['bordersize'] = $a[8];
				$var['resource']['bordercolor'] = $a[9];
				$var['resource']['bgcolor'] = $a[10];
				
				$var['copyright']['text'] = $a[11];
				$var['copyright']['color'] = $a[12];
				$var['copyright']['shadow'] = $a[13];
				$var['copyright']['bgcolor'] = $a[14];
				$var['copyright']['bgshadow'] = $a[15];
				$var['copyright']['bordersize'] = $a[16];
				$var['copyright']['bordercolor'] = $a[17];
				$var['copyright']['position'] = $a[18];
				
				$var['logo']['position'] = $a[19];
				$var['logo']['image'] = $a[20]; 
				
				return $var;
			}else return false;
		}
		
		/**
		 * File Name Parser function
		 *
		 * @param url string $s file name path
		 * @param string $op parse option dirname | basename | extension | filename
		 * @return string option out
		 */
		function filename($s, $op = 'filename'){
			if(!$s || $s == '') return false;
			if(!$op || $op == '') return false;
			if(!eregi('^(dirname)|(basename)|(extension)|(filename)$',$op)) return false;
			$r = pathinfo($s);
			return $r[$op];
		}
		
		
		/**
		 * Image resize or crop selection and start action function
		 *
		 * @return ture or false
		 */
		function startAction(){
			$v = $this->var;
			if(!is_array($v)) return false;
			if($v['resource']['action'] == 'crop')
				return $this->startCrop();
			else if($v['resource']['action'] == 'resize')
				return $this->startResize();
			else
				return false;			
		}
		
		/**
		 * Image Resizing function
		 *
		 * @return ture or false
		 */
		function startResize(){
			$v = $this->var;
			if(!is_array($v)) return false;
			
			$sw = $v['source']['width'];
			$sh = $v['source']['height'];
			$nw = $v['resource']['width'];
			$nh = $v['resource']['height'];
			$sp = $v['source']['path'];
			if($sw == 0) $sw = 1;
			$ratio=($sh/$sw);
   			if ($sw > $nw || $sh > $nh) {
     			if ($sw > $nw) {
         			$sw_resize = $nw;
         			$sh_resize = $sw_resize*$ratio;
     			}else{
       				$sw_resize = $sw;
       				$sh_resize = $sh;
     			}       
     			
     			if ($sh_resize > $nh) {
       				$sh_resize=$nh;
       				$sw_resize=$sh_resize/$ratio;
     			}      
   			}else{
   				$sh_resize = $sh;
   				$sw_resize = $sw;
   			}
			
			$this->createImage(false, $sw_resize, $sh_resize);
			$this->getBackground($v['resource']['bgcolor']);
				
			$source = $this->sourceImage($v['source']['path'], $v['source']['ex']);
			
			@ imagecopyresized($this->imgobj, $source, 0, 0, 0, 0, $sw_resize, $sh_resize, $sw, $sh);
			
			$this->setCopyright($sw_resize, $sh_resize);
			$this->setLogo($sw_resize, $sh_resize);
			$this->setBorder($sw_resize, $sh_resize);
			
			$out = $this->saveImage();
			if($out)
				return true;
			else 
				return false;
						
		}
		
		/**
		 * Image Croping function
		 *
		 * @return ture or false
		 */
		function startCrop(){
			$v = $this->var;
			if(!is_array($v)) return false;
			
			$sw = $v['source']['width'];
			$sh = $v['source']['height'];
			$nw = $v['resource']['width'];
			$nh = $v['resource']['height'];
			$sp = $v['source']['path'];
			if($sw == 0) $sw = 1;
			$this->createImage(false, $nw, $nh);
			$this->getBackground($v['resource']['bgcolor']);
			
			$sourceShape = $this->getShape($sw, $sh);
			
			$nw_offset = $nw+2;
			$nh_offset = $nh+2;	
			
			$ratio=($sh/$sw);
   			if ($sw > $nw || $sh > $nh) {
   				if($sourceShape == 1){
					$sh_resize = $nh_offset;
					$sw_resize = $sh_resize/$ratio;
				}
				
				if($sourceShape == 2 || $sourceShape == 3){
					$sw_resize = $nw_offset;
					$sh_resize = $sw_resize*$ratio;
				}
				
				if($sw_resize == 0) $sw_resize = 1;
				$xratio=($sh_resize/$sw_resize);
				if($sw_resize < $nw_offset){
					$sw_resize = $nw_offset;
					$sh_resize = $sw_resize*$ratio;
				}
				
				if($sh_resize < $nh_offset){
					$sh_resize = $nh_offset;
					$sw_resize = $sh_resize/$ratio;
				}
   			}else{
   				$sh_resize = $sh;
   				$sw_resize = $sw;
   			}
					  			
			$x_offset = ceil(($nw/2)-($sw_resize/2));
			$y_offset = ceil(($nh/2)-($sh_resize/2));
				
			$source = $this->sourceImage($v['source']['path'], $v['source']['ex']);
			
			@ imagecopyresized($this->imgobj, $source, $x_offset, $y_offset, 0, 0, $sw_resize, $sh_resize, $sw, $sh);
			
			$this->setCopyright($nw, $nh);
			$this->setLogo($nw, $nh);
			$this->setBorder($nw, $nh);
			
			$out = $this->saveImage();
			if($out)
				return true;
			else 
				return false;
						
		}
		
		/**
		 * Image Save Function
		 *
		 * @return true or false
		 */
		function saveImage(){
			if(!$this->imgobj) return false;
			$var = $this->var;
			$q = $var['resource']['quality'];
			$q = ($q <= 50)?50:(($q >= 100)?100:80);
			if($var['resource']['path'] != ''){
				$resource = $var['resource']['path'];
				$e = explode('.', $resource);
				$ext = $e[count($e)-1];
				if(!$ext){
					@ header("Content-Type: image/png");
					@ ImagePng($this->imgobj);
					@ imagedestroy($this->imgobj);
				}else if(eregi("^(png)|(PNG)$", $ext)){
					@ ImagePng($this->imgobj, $resource, $q);
					@ imagedestroy($this->imgobj);
				}else if(eregi("^(jpg)|(JPG)|(JPEG)$", $ext)){
					@ imagejpeg($this->imgobj, $resource, $q);
					@ imagedestroy($this->imgobj);
				}else if(eregi("^(gif)|(GIF)$", $ext)){
					@ imagegif($this->imgobj, $resource, $q);
					@ imagedestroy($this->imgobj);
				}else{
					@ header("Content-Type: image/png");
					@ ImagePng($this->imgobj);
					@ imagedestroy($this->imgobj);
				}
			}else{
				$ext = $this->imgtip;
				if(!$ext){
					@ header("Content-Type: image/png");
					@ ImagePng($this->imgobj);
					@ imagedestroy($this->imgobj);
				}else if(eregi("^(png)|(PNG)$", $ext)){
					@ header("Content-Type: image/png");
					@ ImagePng($this->imgobj);
					@ imagedestroy($this->imgobj);
				}else if(eregi("^(jpg)|(JPG)|(JPEG)$", $ext)){
					@ header("Content-Type: image/jpeg");
					@ imagejpeg($this->imgobj);
					@ imagedestroy($this->imgobj);
				}else if(eregi("^(gif)|(GIF)$", $ext)){
					@ header("Content-Type: image/gif");
					@ imagegif($this->imgobj);
					@ imagedestroy($this->imgobj);
				}else{
					@ header("Content-Type: image/png");
					@ ImagePng($this->imgobj);
					@ imagedestroy($this->imgobj);
				}
			}
			return true;
		}
		
		/**
		 * Image Object Creator
		 *
		 * @param string $root file root
		 * @param int $w image width
		 * @param int $h image height
		 * @return imgobj
		 */
		function createImage($root = false, $w = false, $h = false){
			if($root)
				$ext = $this->filename($root,'extension');
			if(!$ext && $w && $h){
				$imgobj = @ imagecreatetruecolor($w, $h);
				$this->imgtip = 'true';
			}else if(eregi("^(png)|(PNG)$", $ext)){
				$imgobj = @ imagecreatefrompng($root);
				$this->imgtip = 'png';
			}else if(eregi("^(jpg)|(JPG)|(JPEG)$", $ext)){
				$imgobj = @ imagecreatefromjpeg($root);
				$this->imgtip = 'jpg';
			}else if(eregi("^(gif)|(GIF)$", $ext)){
				$imgobj = @ imagecreatefromgif($root);
				$this->imgtip = 'gif';
			}else{
				$imgobj = false;
				$this->imgtip = false;
			}
			if($imgobj) @ imagealphablending($imgobj, 1);
			$this->imgobj = $imgobj;
			
			return $imgobj;
		}
		
		/**
		 * Source Image Create
		 *
		 * @param string $root
		 * @return obj
		 */
		function sourceImage($root, $ext = false){
			if($root && !$ext)
				$ext = $this->filename($root,'extension');			
			if(eregi("^(png)|(PNG)$", $ext))
				$imgobj = @ imagecreatefrompng($root);
			else if(eregi("^(jpg)|(JPG)|(JPEG)$", $ext))
				$imgobj = @ imagecreatefromjpeg($root);
			else if(eregi("^(gif)|(GIF)$", $ext))
				$imgobj = @ imagecreatefromgif($root);
			else
				$imgobj = false;
			
			@ imagealphablending($imgobj, 1);
			return $imgobj;
		}
		
		/**
		 * Set Border on the image
		 *
		 * @param int $w new image width
		 * @param int $h new image height
		 * @return true or false
		 */
		function setBorder($w, $h){
			$v = $this->var;
			if(!is_array($v)) return false;
			if($v['resource']['bordersize'] <= 0) return false;
			
			$bs = $v['resource']['bordersize'];
			if(is_array($this->rgb2hex2rgb($v['resource']['bordercolor'])))
				$bordercolor = $this->getColor($v['resource']['bordercolor']);
			else 
				$bordercolor = $this->getColor("#000000");
			
			$x = 0;
			$y = 0;
			//$bs = 10;
				
			//top
			$top = array(		$x, $y,
           						$x, ($y+$bs),
           						($x+$w),($y+$bs),
								($x+$w),$y
           				);
			@ imagefilledpolygon($this->imgobj, $top, (count($top)/2), $bordercolor);
						
			//Left
			$left = array(		$x, $y,
           						$x, ($y+$h),
           						($x+$bs),($y+$h),
								($x+$bs),$y
           					);
			@ imagefilledpolygon($this->imgobj, $left, (count($left)/2), $bordercolor);
			
			//Right
			$bs += 1;
			$right = array(		($x+($w-$bs)), $y,
           						($x+($w-$bs)), ($y+$h),
           						($x+$w),($y+$h),
								($x+$w),$y
           					);
			@ imagefilledpolygon($this->imgobj, $right, (count($right)/2), $bordercolor);
			
			//Bottom
			$bottom = array(	$x, ($y+($h-$bs)),
           						$x, ($y+$h),
           						($x+$w),($y+$h),
								($x+$w),($y+($h-$bs))
           					);
			@ imagefilledpolygon($this->imgobj, $bottom, (count($bottom)/2), $bordercolor);
			
			return true;
		}
		
		/**
		 * Set external logo on the image
		 *
		 * @param int $w new image width
		 * @param int $h new image height
		 * @return true or false
		 */
		function setLogo($w, $h){
			$v = $this->var;
			if(!is_array($v)) return false;
			if($v['logo']['image'] == '' || !file_exists($v['logo']['image'])) return false;
			
			$filename = getimagesize($v['logo']['image']);
			$width = $filename[0];
			$height = $filename[1];
			
			$position = $this->getPositon($v['logo']['position'], $w, $h, $width, $height);
			
			$logo = $this->sourceImage($v['logo']['image']);
			if(!$logo) return false;
			
			@ imageCopyMerge($this->imgobj, $logo, $position['x'], $position['y'], 0, 0, $width, $height,100);
			
			return true;
		}
		
		/**
		 * Set Copyright write to on the image
		 *
		 * @param int $w new image width
		 * @param int $h new image height
		 * @return ture or false
		 */
		function setCopyright($w, $h){
			$v = $this->var;
			
			if(!is_array($v)) return false;
			if($v['copyright']['text'] == '') return false;
			
			
			$box = @ imagettfbbox(($this->fontsize), 0, $this->copyfont, $v['copyright']['text']);
			$height = abs($box[5]-$box[1])+10;
			$width = abs($box[4]-$box[0])+10;
   			$bg_position = $this->getPositon($v['copyright']['position'], $w, $h, $width, $height);
			
			$x = $bg_position['x'];
			$y = $bg_position['y'];
			
			$bgshadow = $this->rgb2hex2rgb($v['copyright']['bgshadow']);
			if(is_array($bgshadow)){
				$bgshadow_coordinate = array(	$x, $y,
           										$x, ($y+$height+2),
           										($x+$width+2),($y+$height+2),
												($x+$width+2),$y
           									);
				@ imagefilledpolygon($this->imgobj, $bgshadow_coordinate, (count($bgshadow_coordinate)/2), $this->getColor($v['copyright']['bgshadow']));
			}
			
			$bgcolor = $this->rgb2hex2rgb($v['copyright']['bgcolor']);
			if(is_array($bgcolor)){				
				$bg_coordinate = array(	$x, $y,
           								$x, ($y+$height),
           								($x+$width),($y+$height),
										($x+$width),$y
           								);
				@ imagefilledpolygon($this->imgobj, $bg_coordinate, (count($bg_coordinate)/2), $this->getColor($v['copyright']['bgcolor']));
			}
			
			if($v['copyright']['bordersize'] > 0){
				$bs = $v['copyright']['bordersize'];
				if(is_array($this->rgb2hex2rgb($v['copyright']['bordercolor'])))
					$bordercolor = $this->getColor($v['copyright']['bordercolor']);
				else 
					$bordercolor = $this->getColor("#000000");

				//top
				$top = array(		$x, $y,
	           						$x, ($y+$bs),
	           						($x+$width),($y+$bs),
									($x+$width),$y
	           				);
				@ imagefilledpolygon($this->imgobj, $top, (count($top)/2), $bordercolor);
							
				//Left
				$left = array(		$x, $y,
	           						$x, ($y+$height),
	           						($x+$bs),($y+$height),
									($x+$bs),$y
	           					);
	           					
				@ imagefilledpolygon($this->imgobj, $left, (count($left)/2), $bordercolor);
				
				//Right
				//$bs += 1;
				$right = array(		($x+($width-$bs)), $y,
	           						($x+($width-$bs)), ($y+$height),
	           						($x+$width),($y+$height),
									($x+$width),$y
	           					);
				@ imagefilledpolygon($this->imgobj, $right, (count($right)/2), $bordercolor);
				
				//Bottom
				$bottom = array(	$x, ($y+($height-$bs)),
	           						$x, ($y+$height),
	           						($x+$width),($y+$height),
									($x+$width),($y+($height-$bs))
	           					);
				@ imagefilledpolygon($this->imgobj, $bottom, (count($bottom)/2), $bordercolor);
			}
			
			if($v['copyright']['text'] != ''){
				$text_color = $this->rgb2hex2rgb($v['copyright']['color']);
				$totalcolor = ($bgcolor[0]+$bgcolor[1]+$bgcolor[2]);
				if(is_array($text_color)){
					$color = $v['copyright']['color'];
				}else if ($totalcolor <= 380) {
					$color = '#FFFFFF';
				}else if ($totalcolor > 380) {
					$color = '#000000';
				}else {
					$color = '#000000';
				}
				$tw = abs($box[4]-$box[0]);
				$tx = ($x+ceil((($width)/2) - (($tw)/2)));
				$ty = ($y+ceil($height/2)+5);
				$shadow = $this->rgb2hex2rgb($v['copyright']['shadow']);
				if(is_array($shadow)){
					@ imagettftext($this->imgobj, $this->fontsize, 0, $tx+1, $ty+1, $this->getColor($v['copyright']['shadow']), $this->copyfont, $v['copyright']['text']);
				}
				@ imagettftext($this->imgobj, $this->fontsize, 0, $tx, $ty, $this->getColor($color), $this->copyfont, $v['copyright']['text']);
			}
			
			return true;
			
		}
		
		function getPositon($pos, $w, $h, $ow, $oh){
			if($pos == 'topleft'){
				$x = $this->padding_left;
				$y = $this->padding_top;
			}elseif ($pos == 'topright'){
				$x = ($w-($ow+$this->padding_right));
				$y = $this->padding_top;
			}elseif ($pos == 'top'){
				$x = ceil((($w-$this->padding_left)/2)-($ow/2));
				$y = $this->padding_top;
			}elseif ($pos == 'left'){
				$x = $this->padding_left;
				$y = ceil((($h-$this->padding_top)/2)-($oh/2));
			}elseif ($pos == 'right'){
				$x = ($w-($ow+$this->padding_right));
				$y = ceil((($h-$this->padding_top)/2)-($oh/2));
			}elseif ($pos == 'center'){
				$x = ceil((($w-$this->padding_left)/2)-($ow/2));
				$y = ceil((($h-$this->padding_top)/2)-($oh/2));
			}elseif ($pos == 'bottomleft') {
				$x = $this->padding_left;
				$y = ($h-($oh+$this->padding_bottom));
			}elseif ($pos == 'bottomright') {
				$x = ($w-($ow+$this->padding_right));
				$y = ($h-($oh+$this->padding_bottom));
			}elseif ($pos == 'bottom') {
				$x = ceil((($w-$this->padding_left)/2)-($ow/2));
				$y = ($h-($oh+$this->padding_bottom));
			}else {
				$x = 2;
				$y = 2;
			}
			
			return array(x=>$x, y=>$y);
		}
		
		/**
		 * Set Main background color
		 *
		 * @param hex dec $color
		 * @return true false
		 */
		function getBackground($color){
			if(!$color) return false;
			$c = $this->getColor($color);
			if(!$c) return false;
			@ imagefill($this->imgobj, 0, 0, $c);
		}
		
		/**
		 * Get Color Obj
		 *
		 * @param hex dec $color
		 * @return obj
		 */
		function getColor($color){
			if(!$color) return false;
			$c = $this->rgb2hex2rgb($color);
			if(!is_array($c)) return false;
			return @ imagecolorallocate($this->imgobj, $c['r'], $c['g'], $c['b']);
		}
		
		/**
		 * RGB to HEX - HEX to RGB
		 *
		 * @param hex dec $c
		 * @return array hex dec
		 */
		function rgb2hex2rgb($c){
			if(!$c) return false;
			$c = trim($c);
			$out = false;
			if(eregi("^[0-9ABCDEFabcdef\#]+$", $c)){
				$c = str_replace('#','', $c);
				$l = strlen($c);
				if($l == 3){
					unset($out);
					$out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0,1));
					$out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 1,1));
					$out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 2,1));
				}elseif($l == 6){
					unset($out);
					$out[0] = $out['r'] = $out['red'] = hexdec(substr($c, 0,2));
					$out[1] = $out['g'] = $out['green'] = hexdec(substr($c, 2,2));
					$out[2] = $out['b'] = $out['blue'] = hexdec(substr($c, 4,2));
				}else $out = false;
				
			}elseif (eregi("^[0-9]+(,| |.)+[0-9]+(,| |.)+[0-9]+$", $c)){
				if(eregi(",", $c))
					$e = explode(",",$c);
				else if(eregi(" ", $c))
					$e = explode(" ",$c);
				else if(eregi(".", $c))
					$e = explode(".",$c);
				else return false;
				
				if(count($e) != 3) return false;
				
				$out = '#';
				for($i = 0; $i<3; $i++)
					$e[$i] = dechex(($e[$i] <= 0)?0:(($e[$i] >= 255)?255:$e[$i]));
				
				for($i = 0; $i<3; $i++)
					$out .= ((strlen($e[$i]) < 2)?'0':'').$e[$i];
					
				$out = strtoupper($out);
			}else $out = false;
			
			return $out;
		}
		
		/**
		 * Image Shape Return
		 *
		 * @param int $w image width
		 * @param int $h image height
		 * @return int 1=>horizontal , 2=>vertical , 3=>square , 0=>unknown
		 */
		function getShape($w, $h){
			if($w > $h)
				return 1;
			elseif ($w < $h)
				return 2;
			elseif ($w == $h)
				return 3;
			else return 0;
		}
	}
	
?>