Location: PHPKode > scripts > PictText > picttext/class_pictText.php
<?php
require_once 'class_dir.php';
/****************************************************************
*****************************************************************

class_pictText.php: make a picture with words
Copyright (C) 2003  Matthieu MARY
http://www.phplibrairies.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

You can found more information about GPL licence at:
http://www.gnu.org/licenses/gpl.html

for contact me:   http://www.phplibrairies.com
****************************************************************
****************************************************************/
/**
 * create the : february 9th 2005.
 * @author      Matthieu MARY
 * version      0.1
 */
class pictText
{

	/**
	 * the image
	 * @type mixed
	 * @private
	 **/
	private $img;
	/**
	 * the ttf font file
	 * @type string
	 * @private
	 **/
	private $font;
	/**
	 * the average font size
	 * @type int
	 * @private
	 **/
	private $fontSize;
	
	/**
	 * the image size
	 * @type Array
	 * @private
	 **/
	private $imgSize;
	
	/**
	 * the image type
	 * @type String
	 * @private
	 **/
	private $type;

	/**
	 * the text to build
	 * @type String
	 * @private
	 **/
	private $text;

	/**
	 * path to ttf fonts folders
	 * @type String
	 * @private
	 **/
	private $fontsFolder;

	/**
	 * path to background picture folder
	 * @type String
	 * @private
	 **/
	private $backgroundFolder;
	
	//--------------------------------------------
	// CONSTRUCT AND DESTRUCT METODS
	//--------------------------------------------

	/**
	 * builder
	 * @param int width : the picture width
	 * @param int height : the picture height
	 * @public
	 * @type Void
	 **/
	public function __construct($width=200,$height=100){
	    $this->setSize($width,$height);
	    $this->img = null;
	    $this->type = 'png';
	    $this->text = '';
	    $this->fontsFolder = './Fonts/';
	    $this->backgroundFolder = './Background/';
	    $this->font = '';
	    $this->fontSize = 15;
	}
	
	public function __destruct(){
	    if ($this->img) imagedestroy($this->img);
	}

	//--------------------------------------------
	// SET METODS
	//--------------------------------------------

	/**
	 * set the picture size
	 * @param int width : it's width
	 * @param int height : it's height
	 * @public
	 * @type Void
	 **/
	public function setSize($width=440,$height=300){
	    $this->imgSize = array(
			'width' => intval($width),
			'height' => intval($height));
	}

	/**
	 * set the average font size
	 * @param int size : the average font size
	 * @public
	 * @type Void
	 **/
	public function setAverageFontSize($size){
	    $this->fontSize = intval($size);
	}
	
	/**
	 * set the picture generated type
	 * @param String type : the of the picture; expected png of jpeg
	 * @public
	 * @type Void
	 **/
	public function setType($type){
	    $type = strtolower($type);
	    if(in_array($type,array('png','jpg'))) $this->type = $type;
	
	}

	/**
	 * set the text to show
	 * @param String text : the text to show
	 * @public
	 * @type Void
	 **/
	public function setText($text){
	    $this->text = strval($text);
	}

	/**
	 * set the ttf fonts folder
	 * @param String folder : the folder to ttf fonts files
	 * @public
	 * @type Void
	 **/
	public function setFontsFolder($folder){
	    if (file_exists($folder) && is_dir($folder)) $this->fontsFolder = $folder;
	}

	/**
	 * set the ttf fonts folder
	 * @param String folder : the folder to ttf fonts files
	 * @public
	 * @type Void
	 **/
	public function setBackgroundFolder($folder){
	    if (file_exists($folder) && is_dir($folder)) $this->backgroundFolder = $folder;
	}
	
	/**
	 * set the ttf font file to use
	 * @param String fontPath : the font path
	 * @public
	 * @type Void
	 **/
	public function setFont($fontPath){
	    $infos = pathinfo($fontPath);
	    if (file_exists($fontPath)&& (strtolower($infos['extension']) == 'ttf')) $this->font = $fontPath;
	    unset($infos);
	}

	//--------------------------------------------
	// GET FUNCTIONS
	//--------------------------------------------
	/**
	 * return the text print in the picture
	 * @type String
	 * @public
	 **/
	public function getText(){
		return $this->text;
	}

	//--------------------------------------------
	// OTHERS FUNCTIONS
	//--------------------------------------------
	/**
	 * return width and height of a ttf character
	 * @param string font : the font file
	 * @param string char : the character
	 * @param int size : the font size
	 * @param int angle : the angle
	 * @private
	 * @type array
	 **/
	private function _getSize($font,$char,$size,$angle){
	    $values = imagettfbbox($size,$angle,$font,$this->text[$i]);
	    return array(($values[2]-$values[6]),($values[5]-$values[1]));
	}
	
	/**
	 * make a random string of chararacters
	 * @param int nbChar : the char length of this text
	 * @public
	 * @type Void
	 **/
	public function makeRandomText($nbChar=6){
	    $nbChar = intval($nbChar);
	    $this->text = '';
	    for($i = 0; $i < $nbChar;$i++){
	        $this->text .= chr(rand(48,122));
	    }
	}

	/**
	 * make the ouput
	 * @public
	 * @type Void
	 **/
	public function show(){
	    $this->img = imagecreatetruecolor($this->imgSize['width'],$this->imgSize['height']) or die ("Can't load GD");
		$nbChars = strlen($this->text); // number of characters in text
        $white = imagecolorallocate($this->img, 255,255,255);
		$black = imagecolorallocate($this->img, 0,0,0);
		$fonts = array(); // ttf font list
		$background = array(); // background pictures list
		$values = array();
		$spacing = 10;
		$font = ''; // current ttf font use
        $size = 0; // current size font
        $angle = 0; // the current font angle
        $fontSizeMax = (1.5*$this->fontSize);
        $fontSizeMin = (0.5*$this->fontSize);

		//------------------------
		// get the font file list
		//------------------------
		if (empty($this->font)){
			$dir = new dir($this->fontsFolder);
			$fonts = $dir->LIST_files('--e ttf');
			unset($dir);
		}
		$maxFonts = (count($fonts)-1);
		//-----------------------------
		// get the background file list
		//-----------------------------
		$dir = new dir($this->backgroundFolder);
		$background = $dir->LIST_files('--e jpeg, png, gif');
		unset($dir);
		//-------------------------
		// Set the background file
		//-------------------------
		if (count($background)>0){
		    $backgroundPict = $background[rand(0,(count($background)-1))];
		    $infos = @getimagesize($backgroundPict);
		    $imgbackground = false;
			switch($infos[2]){
			    case 3:
			       $imgbackground = imagecreatefrompng($backgroundPict);
			       break;
                case 2:
			       $imgbackground = imagecreatefromjpeg($backgroundPict);
			       break;
                case 1:
			       $imgbackground = imagecreatefromgif($backgroundPict);
			       break;
			}
		    if ($imgbackground) imagecopyresized($this->img,$imgbackground,0,0,0,0,$this->imgSize['width'],$this->imgSize['height'],$infos[0],$infos[1]);
			unset($infos);
			unset($backgroundPict);
		}
	
		//-------------------------
		// get the y starting pos
		//-------------------------
        $startingy = rand(0,$this->imgSize['height']);
        $miny = $startingy;
        $maxy = ($this->imgSize['height']/2);
        if ($startingy > ($this->imgSize['height']/2)){
            $maxy = $startingy;
            $miny = ($this->imgSize['height']/2);
        }
        
        //-------------------------
		// Set each characters
		//-------------------------
		for($i = 0; $i < $nbChars;$i++){
			$size = rand($fontSizeMin,$fontSizeMax);
			$angle = rand(0,60);
			$font = (empty($this->font)?$fonts[rand(0,$maxFonts)]:$this->font);
			$y = rand($miny,$maxy);
			imagettftext($this->img,$size,$angle,$spacing, $y, $white, $font,$this->text[$i]);
            $values = $this->_getSize($font,$this->text[$i],$size,$angle);
            $spacing += $values[0]+10;
		}
		unset($values);
		unset($angle);
		unset($size);
		unset($y);
		unset($spacing);
		//-------------------------
		// send the output
		//-------------------------
		switch($this->type){
		   	case 'jpeg':
		   	   if (!headers_sent()) header ("Content-type: image/jpeg");
			    imagejpeg($this->img);
			    break;
            case 'png':
		    default:
		      if (!headers_sent()) header ("Content-type: image/png");
                imagepng($this->img);
		        break;
		}
	}
}
?>
Return current item: PictText