Zend - The PHP Company




Graphics

Add Code


Class to draw Text as PNG Graphic  

Type: class
Added by: hermannus
Entered: 25/10/2000
Last modified: 01/12/2000
Rating: - (fewer than 3 votes)
Views: 10561
Class to draw a text message as a PNG graphic. The text message, font, colour, background, size, padding, rotation, and transparency can all be defined via OO interface. Code below can be called as a URL.


<?
/*
TextPNG Class. Class to draw text using TTF font and output PNG graphic.
Copyright (C) 2000  Herman Veluwenkamp

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

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

Copy of GNU Lesser General Public License at: http://www.gnu.org/copyleft/lesser.txt
Contact author at: hermanV@mindless.com

*/

/*
To draw the PNG - call this script with a URL like the following:

http://www.yoursite.com/text_png.php3?msg=testing+class&rot=15&size=48&font=fonts/ARIAL.TTF
*/


class textPNG {
    var 
$font 'TIMES.TTF'//default font. put in full path.
    
var $msg "undefined"// default text to display.
    
var $size 24;
    var 
$rot 0// rotation in degrees.
    
var $pad 0// padding.
    
var $transparent 1// transparency set to on.
    
var $red 0// white text...
    
var $grn 0;
    var 
$blu 0;
    var 
$bg_red 255// on black background.
    
var $bg_grn 255;
    var 
$bg_blu 255;

function 
draw() {
    
$width 0;
    
$height 0;
    
$offset_x 0;
    
$offset_y 0;
    
$bounds = array();
    
$image "";
    
    
//$this->msg = date ("h:i:s");
    
    // determine font height.
    
$bounds ImageTTFBBox($this->size$this->rot$this->font"W");
    if (
$this->rot 0) {
        
$font_height abs($bounds[7]-$bounds[1]);        
    } else if (
$this->rot 0) {
        
$font_height abs($bounds[1]-$bounds[7]);
    } else {
        
$font_height abs($bounds[7]-$bounds[1]);
    }

    
// determine bounding box.
    
$bounds ImageTTFBBox($this->size$this->rot$this->font$this->msg);
    if (
$this->rot 0) {
        
$width abs($bounds[4]-$bounds[0]);
        
$height abs($bounds[3]-$bounds[7]);
        
$offset_y $font_height;
        
$offset_x 0;
        
    } else if (
$this->rot 0) {
        
$width abs($bounds[2]-$bounds[6]);
        
$height abs($bounds[1]-$bounds[5]);
        
$offset_y abs($bounds[7]-$bounds[5])+$font_height;
        
$offset_x abs($bounds[0]-$bounds[6]);
        
    } else {
        
$width abs($bounds[4]-$bounds[6]);
        
$height abs($bounds[7]-$bounds[1]);
        
$offset_y $font_height;;
        
$offset_x 0;
    }
    
    
$image imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
    
    
$background ImageColorAllocate($image$this->bg_red$this->bg_grn$this->bg_blu);
    
$foreground ImageColorAllocate($image$this->red$this->grn$this->blu);
    
    if (
$this->transparentImageColorTransparent($image$background);
    
ImageInterlace($imagefalse);
    
    
// render it.
    
ImageTTFText($image$this->size$this->rot$offset_x+$this->pad$offset_y+$this->pad$foreground$this->font$this->msg);
    
    
// output PNG object.
    
imagePNG($image);
}
}

$text = new textPNG// instantiate new textPNG class.

if (isset($msg)) $text->msg $msg// text to display
if (isset($font)) $text->font $font// font to use (include directory if needed).
if (isset($size)) $text->size $size// size in points
if (isset($rot)) $text->rot $rot// rotation
if (isset($pad)) $text->pad $pad// padding in pixels around text.
if (isset($tr)) $text->transparent $tr// transparency flag (boolean).
if (isset($clr)) { // text colour
  
$text->red hexdec(substr($clr02));
  
$text->grn hexdec(substr($clr22));
  
$text->blu hexdec(substr($clr42));
}
if (isset(
$bg)) { // background colour
  
$text->bg_red hexdec(substr($bg02));
  
$text->bg_grn hexdec(substr($bg22));
  
$text->bg_blu hexdec(substr($bg42));
}



// make up some expiry information. say, 60 secs to ive.
$now mktime (date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));
$expires mktime (date("H"),date("i"),date("s") + 60 ,date("m"),date("d"),date("Y")); // add 60 secs.
$expires_gmt gmdate('D, d M Y H:i:s'$expires).' GMT';
$last_modified_gmt  gmdate('D, d M Y H:i:s'$now).' GMT';

header('Content-type:image/png');
header('Expires: '.$expires_gmt);
header('last-modified: '.$last_modified_gmt);

$text->draw();
?>


Usage Example




Rate This Script





Search



This Category All Categories