Location: PHPKode > scripts > Date PNG > date-png/DatePNG.inc.php
<?php

// author Laknath (blaknath [at] gmail [dot] com)
// version 0.9
// to see a live demo of this class, check http://hotelotravel.com

/*
Functionality:
when given a valid timestamp it creates a png image with the given date format and png background template in the designated folder. To optimize the process and avoid having to create pngs every time the site loads it caches created pngs and use them if a given date png already exists.

Example:

//get font ttf path
$font = $_SERVER["DOCUMENT_ROOT"].'/Arial.ttf';

//png file template that's used as the background
$png_path = $_SERVER["DOCUMENT_ROOT"].'/date.png';

//create a DatePNG object with the current timestamp and png file template
$datePNG = new DatePNG($time(), $png_path);

//define the folder which the font files should be created (this folder must exist and writable)
$datePNG->folder = $_SERVER["DOCUMENT_ROOT"].'/dates';

//define the text style and date pattern, each text pattern in each associated array

//Possible values for the associated array with keys
$datePNG->formats = array(
	array('format'=>'d','font'=> $font, 'size'=>14,'angle'=>0,'x'=>5,'y'=>20,'color'=>'0x666666', 'linespacing' => 1.5),
	array('format'=>'M','font'=> $font, 'size'=>8,'angle'=>0,'x'=>5,'y'=>37,'color'=>'0x666666', 'upper_case' => true)
);

//create the png and return the path of the created date png
$date_path = $datePNG->create_png();

echo '<img src="'.$date_path.'" alt="date" />' ;
*/

/*  
 * Current version
 */
DatePNG::self( 'CLASS_VERSION', ' 0.9' );

/*  
 * Author
 */
DatePNG::self( 'CLASS_AUTHOR', 'Laknath' );

/*  
 * Contact
 */
DatePNG::self( 'CLASS_CONTACT', 'blaknath [at] gmail [dot] com');

/*  
 * Class description
 */
DatePNG::self( 'CLASS_DESCRIPTION', 'Present date as a PNG' );


/* 
 * Date as PNG class
 */
class DatePNG
{
    /* 
	 * PNG file
	 */
    var $png;

    /* 
	 * date timestamp
	 */
    var $time;

    /*
	 * folder to store images
	 */
    var $folder = 'dates';

	/*    		
     * date format
	 * Should enter the required text with needed font parameteres

	 * Give values as a associated array
	 * FORMAT => the date format according to http://www.php.net/date
	 * FONT => the path to the font file (including the font file)
	 * SIZE => size of the text to be printed
	 * ANGLE => angle of which the font should be printed - default 0 (no angle)
	 * X => x position of the bottom left of the font should start
	 * Y => y position of the bottom left of the font should start		
	 * color => Hex value of the font colour	

	 * Note:
	 * Default values assumes true type font Arial.ttf file is in the root directory		
	*/	
	var $formats =  array(
		 				array('format'=>'d','font'=> 'Arial.ttf', 'size'=>'20','angle'=>'0','x'=>4,'y'=>4,'color'=>'0x00'),
						array('format'=>'M','font'=> 'Arial.ttf','size'=>'10','angle'=>'0','x'=>4,'y'=>25,'color'=>'0x00'
					));
	
    /* 
	 * full formatted file name
	 */ 
	var $display_date;
			

    /**
	 *	Class constructor
     *  @param $def_no (int) Default error code
     *  @param $def_msg (string) Default error message
     */

    function DatePNG($time, $png = 'date.png')
    {
         $this->time = $time;
         $this->png = $png;
    }
	
	
	/*
	 * Create the png from a given date
	 *
	 * Return the path of the created/existing date image
	 */
	 function create_png(){		
		
		$path = $this->get_date_path();
				
		if (!$this->date_exists()){		
	
			$im = @imagecreatefrompng($this->png);
			
			if (is_array($this->formats)){			
				foreach ($this->formats as $format){
					
					$font = isset($format['font']) ? $format['font'] : 'Arial.ttf' ;
					$size = isset($format['size']) ? $format['size'] : 10 ;				
					$angle = isset($format['angle']) ? $format['angle'] : 0 ;				
					$x = isset($format['x']) ? $format['x'] : 4 ;
					$y = isset($format['y']) ? $format['y'] : 4 ;
					$color = isset($format['color']) ? $format['color'] : '0x000000' ;		
					$format = isset($format['format']) ? date($format['format'],$this->time) : date('dM',$this->time) ;
					$format = $format['upper_case'] ? strtoupper($format) : $format ;
					$linespace = isset($format['linespacing']) ? array('linespacing' => $format['linespacing']) : array('linespacing' => 1.0);
					
					@imagefttext($im, $size, $angle, $x, $y, $color, $font, $format, $linespace);
				}
			}
			
			// Save the image to file and free memory
			@imagepng($im, $path, 0, NULL);
			@imagedestroy($im);
		}
		
		if (strpos($path,$_SERVER["DOCUMENT_ROOT"]) === 0 ){
			$path = str_replace($_SERVER["DOCUMENT_ROOT"],'',$path);
		}
		
		return $path;
	 }
	
	/* 
	 * Checks if the given date exists as a png
	 *
	 * Return if file exists or not (boolean)
	 */	
	function date_exists(){
		
		$path = $this->get_date_path();	
				
		if (file_exists($path)){
			return true;
		}else{
			return false;
		}
	}

	/* 
	 * Returns the full path to the date image
	 *
	 */	
	function get_date_path(){
		$this->get_display_date();
		return $this->folder . '/' . $this->display_date . '.png';	
	}

	/* Returns display date
	 *
	 */
	 function get_display_date(){

		$this->display_date = null;
	 
		foreach ($this->formats as $format){
			$date_part = isset($format['format']) ? $format['format'] : 'd' ;
			$this->display_date .= date($date_part,$this->time);
		}
	 
	 	return $this->display_date;
	 }

	/* 
	 * Handle meta data about the class
	 *
	 * @param $name (String) Name of the property
	 * @param $value (Mix) Value of the  property
	 *
	 */
	 
    function &self($name = NULL, $value = NULL)
    {
        static $self;

        if( !is_array($self) )
        {
            $self = array();
        }

        if( is_null($name) && is_null($value) )
        {
            return $self;	
        } 
		elseif( !is_null($name) && is_null($value) )
        {
            return (isset($self[$name]) ? $self[$name] : NULL);
        } 
		else
        {
            $self[$name] = $value;
        }
    }
}

?>
Return current item: Date PNG