Location: PHPKode > scripts > ImageTitle > imagetitle/imagetitles.php
<?php
	/* Developed by Vlad Balmos
	    Copyright (C) 2008 hide@address.com Yahoo ID: vvlladd
	    Use this class anyway you want but please give me credit.
	    No restrictions.
	 */
	 
	 
	/* Generates Image titles using GD library */
	class ImageTitle {
		private $tmpDir = '/var/www/images/phptitles/';   // the location where the generated images will reside. Replace this with your path. You will need write permisssion for the location
		private $imagesDir = '/images/phptitles/'; // starting url of the image, relative to the document root. Replace this with your own path. 
		private $pathToFont = '/path/to/font/file/'; // path to font file. Replace this with your own
		private $font;	// font file
		private $type 		= 'jpg'; // image type. Possible values are: 'jpg', 'gif', 'png'
		private $fontSize 		= 12; // font size
		private $widthIncrement = 6;  // value used to increase the determined image width
		private $heightIncrement = 6; // value used to increase the determined image height
		private $color 		= array('r' => 0, 'g' => 0, 'b' => 0);  // foreground color
		private $bgcolor	       = array('r' => 255, 'g' => 255, 'b' => 255);  // background color
		
		/* Class constructor 
			$font - (string) the font file
			$pathToFont - (string) absolute path to font file
			$type - (string) image type:  'jpg', 'gif', or 'png'
			$color - (string) in hex format
			$bgcolor - (string) in hex format
			$widthIncrement - (int)
			$heightIncrement - (int)
		 */
		public function __construct($font, $type = null, $fontSize = null, $color = null, $bgcolor = null, $widthIncrement = null, $heightIncrement = null) {
			$this->font 		  = $font;
			$this->type 		  = ($type) ? $type : $this->type;
			$this->fontSize 	  = ($fontSize) ? $fontSize : $this->fontSize;
			$this->color 		  = ($color) ? $this->hex2rgb($color) : $this->color;
			$this->bgcolor 	  = ($bgcolor) ? $this->hex2rgb($bgcolor) : $this->bgcolor;
			$this->heightIncrement = ($heightIncrement) ? $heightIncrement : $this->heightIncrement;
			$this->widthIncrement = ($widthIncrement) ? $widthIncrement : $this->widthIncrement;
		}
		
		/* Can display a set of images or return their url
		    $string - (string) - the title string,
		    $brake - (int) if the text is longer then $brake characters, the method will break the text into separate images, 
		    $attributes - (string) <img> tag attributes (style, js events, id, class, etc)
		    $return - (bool) if set to TRUE it will return an array containing urls of the images, instead of displaying the images
		    $transparent - (bool) if set to TRUE, will create transparent images. Works only with gif, and png type
		 */
		public function showTitle($string, $brake = 200, $attributes = null, $return = false, $transparent = false) {
			$tags = array();
			$ref_tags =& $tags; 
			$ext = '.jpg';			// set the appropriate extension
			switch($this->type) {
				case 'jpg' : $ext = '.jpg'; break;
				case 'gif'   : $ext = '.gif'; break;
				case 'png' : $ext = '.png'; break;
			}
			$tmp_string = wordwrap($string, $brake, '|');  // brake the string by $brake characters
			$lines = explode('|', $tmp_string);
			
			/* process each line of text */
			foreach($lines as $key => $value) {
				$filename = $this->makeFile($value).$ext;   
				if(file_exists($this->tmpDir.$filename)) {  // if file exists get it from cache
					$tags[] = $this->imagesDir.$filename;
				} else {   // else generate a new image
					switch($this->type) {
						case 'jpg' : $this->createJpeg($ref_tags, $value); break;
						case 'gif'   : $this->createGif($tags,  $value, $transparent); break;
						case 'png' : $this->createPng($tags, $value, $transparent); break;
					}
				}
			}
			if($return) {
				return $tags;
			} else {
				foreach($tags as $key => $value) {
					echo '<img src="'.$value.'" alt="'.$string.'" '.$attributes.'/>';
				}
			}
		}
		/* Create a JPEG image */
		private function createJpeg(&$tags, $value) {
			$filename = $this->makeFile($value).'.jpg';
			$ttfbox = imagettfbbox($this->fontSize, 0,  $this->pathToFont.DIRECTORY_SEPARATOR.$this->font, $value);
			$width = ($ttfbox[2] - $ttfbox[0]) + $this->widthIncrement;
			$height = $this->fontSize + $this->heightIncrement;
			$image = imagecreatetruecolor($width, $height);
			$imageBackgroundColor = imagecolorallocate($image, $this->bgcolor['r'], $this->bgcolor['g'], $this->bgcolor['b']);
			$imagecolor = imagecolorallocate($image, $this->color['r'], $this->color['g'], $this->color['b']);
			imagefill($image, 0, 0, $imageBackgroundColor);
			imagettftext($image, $this->fontSize, 0, 1, $this->fontSize, $imagecolor, $this->pathToFont.DIRECTORY_SEPARATOR.$this->font, $value);
			imagejpeg($image, $this->tmpDir.$filename);
			imagedestroy($image);
			$tags[] = $this->imagesDir.$filename;
		}
		/* Create a GIF image */
		private function createGif(&$tags, $value, $transparent) {
			$filename = $this->makeFile($value).'.gif';
			$ttfbox = imagettfbbox($this->fontSize, 0,  $this->pathToFont.DIRECTORY_SEPARATOR.$this->font, $value);
			$width = ($ttfbox[2] - $ttfbox[0]) + $this->widthIncrement;
			$height = $this->fontSize + $this->heightIncrement;
			$image = imagecreatetruecolor($width, $height);
			imagetruecolortopalette($image, true, 255);
			$imageBackgroundColor = imagecolorallocate($image, $this->bgcolor['r'], $this->bgcolor['g'], $this->bgcolor['b']);
			$imagecolor = imagecolorallocate($image, $this->color['r'], $this->color['g'], $this->color['b']);
			imagefill($image, 0, 0, $imageBackgroundColor);
			imagettftext($image, $this->fontSize, 0, 1, $this->fontSize, $imagecolor, $this->pathToFont.DIRECTORY_SEPARATOR.$this->font, $value);
			if($transparent) {
				imagecolortransparent($image, $imageBackgroundColor);
			}
			imagegif($image, $this->tmpDir.$filename);
			imagedestroy($image);
			$tags[] = $this->imagesDir.$filename;
		}
		/* Create a PNG image */
		private function createPng(&$tags, $value, $transparent) {
			$filename = $this->makeFile($value).'.png';
			$ttfbox = imagettfbbox($this->fontSize, 0,  $this->pathToFont.DIRECTORY_SEPARATOR.$this->font, $value);
			$width = ($ttfbox[2] - $ttfbox[0]) + $this->widthIncrement;
			$height = $this->fontSize + $this->heightIncrement;
			$image = imagecreatetruecolor($width, $height);
			if($transparent) {
				imagesavealpha($image, true);
				imagealphablending($image, false);
				$imageBackgroundColor = imagecolorallocatealpha($image,  255, 255, 255, 127);
			} else {
				$imageBackgroundColor = imagecolorallocate($image, $this->bgcolor['r'], $this->bgcolor['g'], $this->bgcolor['b']);
			}
			$imagecolor = imagecolorallocate($image, $this->color['r'], $this->color['g'], $this->color['b']);
			imagefill($image, 0, 0, $imageBackgroundColor);
			imagettftext($image, $this->fontSize, 0, 1, $this->fontSize, $imagecolor, $this->pathToFont.DIRECTORY_SEPARATOR.$this->font, $value);
			imagepng($image, $this->tmpDir.$filename);
			imagedestroy($image);
			$tags[] = $this->imagesDir.$filename;
		}
		
		/* Utility functions 
		    Self explainatory
		  */
		public function setHeightIncrement($heightIncrement) {
			$this->heightIncrement = $heightIncrement;
		}
		
		public function setWidthIncrement($widthIncrement) {
			$this->widthIncrement = $widthIncrement;
		}
		
		public function setType($type) {
			$this->type = $type;
		}
		
		public function setFont($font) {
			$this->font = $font;
		}
		
		public function setPathToFont($path) {
			$this->pathToFont = $path;
		}
		
		public function setFontSize($size) {
			$this->fontSize = $size;
			$this->height = $size + 4;
		}
		
		public function setImagesDir($dir) {
			$this->imagesDir = $dir;
		}
		
		public function setColor($color) {
			$this->color = $this->hex2rgb($color);
			var_dump($this->color);
		}
		
		public function setBgcolor($color) {
			$this->color = $this->hex2rgb($color);
		}
		
		private function hex2rgb($color) {
			$rgb = array();
			$color = str_replace('#', '', $color);
			$rgb['r'] = hexdec(substr($color, 0, 2));
			$rgb['g'] = hexdec(substr($color, 2, 2));
			$rgb['b'] = hexdec(substr($color, 4, 2));
			return $rgb;
		}
		
		private function makeFile($string) {
			$string = strtolower($string);
			$string = preg_replace('/[^a-z0-9_]/i', '_', $string);
			$string = preg_replace('/_[_]*/i', '_', $string);
			$string = preg_replace('/_$/i', '', $string);
			$string = preg_replace('/^_/i', '', $string);
			return $string;
		}
		
	}
?>
Return current item: ImageTitle