Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
[PHP] GD Library Imager
Old 09-01-2010, 07:56 AM [PHP] GD Library Imager
Average Talker

Posts: 19
Name: Colton
Location: NC, USA
Trades: 0
Since I'm all about sharing code that I've randomly came up with, here's another little project that I've had going on for quite a bit. This is probably the 3rd or 4th fork from the original dirty code that I threw together. This one is actually heading towards completion.

Code:
<?php
        /**
         * PHP GD Library Imager
         *
         * @author Colton
         * @version 0.2
         * @copyright Net Refuge 2010
         */
	class Imager {
		protected $img;
		protected $data;
		public function __construct() {
			$this->setoptions(array(
				'background-color'=>'#FFF',
				'title-color'	=>	'#000',
				'text-color'	=>	'#555',
				'font-size'		=>	12,
				'font-file'		=>	'framd.ttf',
				'caption-pos'	=>	'bottom',
				'caption-box'	=>	true,
				'line-color'	=>	'#FF0',
				'fill-color'	=>	'#0CF',
				'border-width'	=>	0,
				'margin-color'	=>	'#FFF',
				'margin-left'	=>	0,
				'margin-right'	=>	0,
				'margin-bottom'	=>	0,
				'margin-top'	=>	0,
				'opacity'		=>	0
			));
			return $this;
		}
		public function __destruct() {
			$this->output();
		}
		public function set($options,$value=null) {if(is_array($options))$this->setoptions($options);else $this->setoptions(array($options=>$value));return $this;}
		protected function setoptions($options) {if(is_array($options))foreach($options as $k=>$v)$this->options[$k]=$this->hex2rgb($v);}
		protected function check() {if(@get_resource_type($this->img)!='gd')return false;else return true;}
		protected function color($c,$notrans=false) {if(!$this->check())exit;$c=$this->options[$c];if($notrans==false)$c=imagecolorallocatealpha($this->img,$c[0],$c[1],$c[2],$this->options['opacity']);else $c=imagecolorallocate($this->img,$c[0],$c[1],$c[2]); return $c;}
		protected function hex2rgb($hex) {
			if(!is_string($hex))return $hex;
			if(substr($hex,0,1)=="#"){
				$hex=substr($hex,1);
				if(strlen($hex)==6)$rgb=array($hex[0].$hex[1],$hex[2].$hex[3],$hex[4].$hex[5]);
				elseif(strlen($hex)==3)$rgb=array(str_repeat($hex[0],2),str_repeat($hex[1],2),str_repeat($hex[2],2));
				else return $hex;
			} else return $hex;
			foreach($rgb as $k=>$c)$rgb[$k]=hexdec($c);
			return $rgb;
		}
		public function create($w,$h) {
			if ($this->check()) exit;
			$this->data=(object)array(
					'width'		=>	$w,
					'height'	=>	$h,
					'type'		=>	IMAGETYPE_PNG
			);
			$this->img=imagecreatetruecolor($w,$h);
			imagefill($this->img,0,0,$this->color('background-color'));
			return $this;
		}
		public function loadImage($file) {
			if ($this->check()) exit;
			try {
				if(!file_exists($file))throw new Error("File does not exist: {$file}");
				if($this->img=@imagecreatefrompng($file));
				elseif($this->img=@imagecreatefromjpeg($file));
				elseif($this->img=@imagecreatefromgif($file));
				elseif($this->img=@imagecreatefromstring($file));
				else throw new Error("Unable to create image from file. The file could be corrupted, or not recognized by the PHP GD library.");
				$tmp=getimagesize($file);
				$this->data=(object)array(
							'filename'		=>	$file,
							'width'			=>	$tmp[0],
							'height'		=>	$tmp[1],
							'type'			=>	image_type_to_mime_type($tmp[2])
				);
			} catch(Error $err){$err=str_replace("\n","<br />",$err);die("<b>[Imager Error]</b> {$err}");}
			return $this;
		}
		protected function getboundary($text,$size=null) {
			$box=imagettfbbox((!empty($size)?$size:$this->options['font-size']),$this->options['font-angle'],$this->options['font-file'],$text);
			$box=array('min_x'=>min(array($box[0],$box[2],$box[4],$box[6])),
					   'min_y'=>min(array($box[1],$box[3],$box[5],$box[7])),
					   'max_x'=>max(array($box[0],$box[2],$box[4],$box[6])),
					   'max_y'=>max(array($box[1],$box[3],$box[5],$box[7])));
			$box=array('width'=>abs($box['max_x']-$box['min_x']),'height'=>abs($box['max_y']-$box['min_y']));
			return $box;
		}
		public function caption($title,$text,$x,$y) {
			if (!$this->check()) exit;
			$x+=$this->options['border-width'];$y+=$this->options['border-width'];
			$pos[]=$this->getboundary($title);
			$pos[]=$this->getboundary($text,$this->options['font-size']*.7);
			list($w,$h)=array($x,$y);
			if ($this->options['caption-pos']=='right'){$w+=$pos[0]['width']+$pos[1]['width'];$h+=($pos[1]['height']>$pos[0]['height']?$pos[1]['height']:$pos[0]['height']);}
			else {$w+=($pos[0]['width']>$pos[1]['width']?$pos[0]['width']:$pos[1]['width']);$h+=$pos[0]['height']+$pos[1]['height'];}
			if($this->options['caption-box']==true)$this->borderedrectangle($x,$y,$w+$this->options['font-size']+($this->options['caption-pos']=='right'?10:5),$h+$this->options['font-size']+($this->options['caption-pos']=='right'?5:0));
			if($this->options['caption-pos']=='right') imagettftext($this->img,$this->options['font-size']*.7,0,$x+$pos[0]['width']+15,$y+5+$this->options['font-size'],$this->color('text-color'),$this->options['font-file'],$text);
			else imagettftext($this->img,$this->options['font-size']*.7,0,$x+7,$y+2+$this->options['font-size']+$pos[0]['height'],$this->color('text-color'),$this->options['font-file'],$text);
			imagettftext($this->img,$this->options['font-size'],0,$x+5,$y+5+$this->options['font-size'],$this->color('title-color'),$this->options['font-file'],$title.($this->options['caption-pos']=='right'?':':''));
			return $this;
		}
		public function text($text,$x,$y) {
			imagettftext($this->img,$this->options['font-size'],$this->options['font-angle'],$x,$y,$this->color('text-color'),$this->options['font-file'],$text);
			return $this;
		}
		public function verticaltext($text,$x,$y) {
			$tmp=null;
			for ($i=0;$i<=strlen($text);$i++)$tmp.=substr($text,$i,1)."\n";
			$this->text($tmp,$x,$y);
			return $this;
		}
		public function circle($cx,$cy,$radius) {
			imageellipse($this->img,$cx,$cy,$radius,$radius,$this->color('line-color'));
			return $this;
		}
		public function filledcircle($cx,$cy,$radius) {
			imagefilledellipse($this->img,$cx,$cy,$radius,$radius,$this->color('fill-color'));
			return $this;
		}
		public function borderedcircle($cx,$cy,$radius) {
			$this->filledcircle($cx,$cy,$radius);
			$this->options['line-color']=$this->options['border-color'];
			for($i=1;$i<=$this->options['border-width'];$i++) $this->circle($cx,$cy,$radius+$i);
			return $this;
		}
		public function rectangle($x1,$y1,$x2,$y2) {
			imagerectangle($this->img,$x1,$y1,$x2,$y2,$this->color('line-color'));
			return $this;
		}
		public function filledrectangle($x1,$y1,$x2,$y2) {
			imagefilledrectangle($this->img,$x1,$y1,$x2,$y2,$this->color('fill-color'));
			return $this;
		}
		public function borderedrectangle($x1,$y1,$x2,$y2) {
			$this->filledrectangle($x1,$y1,$x2,$y2);
			$this->options['line-color']=$this->options['border-color'];
			for($i=1;$i<=$this->options['border-width'];$i++) $this->rectangle($x1-$i,$y1-$i,$x2+$i,$y2+$i);
			return $this;
		}
		public function createmargins() {
			$size=array($this->data->width+$this->options['margin-left']+$this->options['margin-right'],$this->data->height+$this->options['margin-top']+$this->options['margin-bottom']);
			$tmp=imagecreatetruecolor($size[0],$size[1]);
			imagefill($tmp,0,0,$this->color('margin-color',true));
			imagecopyresampled($tmp,$this->img,$this->options['margin-left'],$this->options['margin-top'],0,0,$this->data->width,$this->data->height,$this->data->width,$this->data->height);
			list($this->img,$this->data->width,$this->data->height)=array($tmp,$size[0],$size[1]);
			$this->set(array('margin-left'=>0,'margin-right'=>0,'margin-top'=>0,'margin-bottom'=>0));
			return $this;
		}
		public function resize($w=null,$h=null) {
			if(empty($w))$w=$this->data->width;
			if(empty($h))$h=$this->data->height;
			$tmp=imagecreatetruecolor($w,$h);
			imagecopyresampled($tmp,$this->img,0,0,0,0,$w,$h,$this->data->width,$this->data->height);
			list($this->img,$this->data->width,$this->data->height)=array($tmp,$w,$h);
			return $this;
		}
		public function output() {
			if(!$this->check())exit;
			header("Content-type: {$this->data->type}");
			imagepng($this->img);
			imagedestroy($this->img);
			return $this;
		}
		public function save($file,$type=0) {
			if (!$this->check()) exit;
			if($type==1)imagegif($this->img,$file);
			elseif($type==2)imagejpeg($this->img,$file,100);
			else imagepng($this->img,$file,9);
			return $this;
		}
	}
	class Error extends Exception {}
?>
This code is not completed. I'm only about 30% done with this code right now. However, it's current functionality may still be able to help with some of your imaging problems.

Usage:
This script allows chain coding. For those of you that don't know PHP OOP, this means that you can call one command after the other (as you can in Javascript objects) without having to use the variable over and over. Example:
Code:
<?php

    include 'imager.php';
    $tmp=new Imager;
    $tmp->set('background-color','#000')->create(500,500)->caption('Caption Title', 'Caption Text', 10, 10);

?>
Feedback and comments welcome, as well as suggestions for things to implement.
Colton is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Reply     « Reply to [PHP] GD Library Imager
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19142 seconds with 12 queries