Location: PHPKode > scripts > TextToImage > texttoimage/TextToImage.php
<?php
/**
 * Author: Nino Paolo Amarillento
 * Email: hide@address.com
 * Version: Alpha
 * Created: Oct. 1, 2009
 * 
 * Usage:
 * The purpose of this class is to convert Text-to-Image. 
 * This will be use especially on displaying email addresses on your website 
 * this will help hide the email addresses from your website to web spiders & crawlers 
 * to help avoid spamming.
 *
 * Requirements:
 * GD, PHP 4+ & 5+
 */
class TextToImage
{
	private $text;		// String "text"
	private $img;		// GD image resource
	private $imgType;	// Image Type (jpg, gif, png)
	private $bgColor;	// Background's Image Color
	private $txtColor;	// Text Color
	private $path;		// Image Diretory. Path where is image will be save.
	private $fontSize = 8;  // Font Size Default is 8
	
	/**
	 **	Constructor
	 ** 	PHP 5
	 **/
	function __construct($text = "")
	{
		$this->setImageType("jpg");	// image type => jpg/gif/png		
		$this->setText($text);		// text string
	}
	
	// PHP 4 
	function TextToImage($text = "")
	{
		$this->__construct($text);
	}
	
	/**
	 **	Getters
	 **/
	function getText()
	{
		return $this->text;
	}
	
	function getImagePath()
	{
		return $this->path;
	}
	
	function getImageType()
	{
		return $this->imgType;
	}
	
	/**
	 ** Setters
	 **/
	function setText($text)
	{
		$this->text = (empty($text)) ? "" : $text;
	}
	
	function setImagePath($path = "./")
	{
		if(substr($path, -1, 1) != "/") $path .= "/";
		$this->path = $path;
	}
	
	//
	// Set Image Type
	//
	function setImageType($type)
	{
		$this->imgType = strtolower($type);
	}
	
	/**
	 ** Behaviors
	 **/
	function create()
	{
		header("Content-type: image/{$this->imgType}");
		$this->render();
		switch($this->imgType)
		{
			case "png" : imagepng($this->img); break;
			case "gif" : imagegif($this->img); break;
			case "jpg" : 
			default: imagejpeg($this->img, null, 100);
		}
	$this->__destruct();
	}
	
	function save($filename = "")
	{
		$filename = (empty($filename)) ? date("YmdHis") : $filename;
		$filename .= ".{$this->imgType}";   // setting filename
		$filename = $this->path.$filename;	// image path
		
		$this->render();
		
		switch($this->imgType)
		{
			case "png" : imagepng($this->img, $filename); break;
			case "gif" : imagegif($this->img, $filename); break;
			case "jpg" : 
			default: imagejpeg($this->img,  $filename, 100);
		}
		
	
	  $this->__destruct();
	  return $filename;
	}
	
	
	private function render()
	{
		
		// Init GD
		// config width and height of the image
		$width = ($this->fontSize + 1.5) * (strlen($this->text));
		$height = $this->fontSize * 3.5;
		// create image, background, and textcolor
		$this->img = imagecreate($width, $height) or DIE("Cannot Initialize new GD image stream");
		
		$this->bgColor = imagecolorallocate($this->img, 255, 255, 255);
		$this->txtColor = imagecolorallocate($this->img, 0, 0, 0);
		imagestring($this->img, $this->fontSize, 5, 5,  $this->text, $this->txtColor);
	}
	
	/**
	 * Destructor
	 */
	function __destruct()
	{
		@imagedestroy($this->img);
	}
}

/**
 *	Helper
 */
if(isset($_GET['texttoimage']) && !empty($_GET['texttoimage']))
{
	$img = new TextToImage($_GET['texttoimage']);
	$img->create();
}

?>
Return current item: TextToImage