Zend - The PHP Company




Graphics

Add Code


Text on Image  

Type: class
Added by: eliteboo
Entered: 04/01/2003
Last modified: 02/12/2002
Rating: - (fewer than 3 votes)
Views: 7595
Very simple class to use to write text on an image. Supports GIF, PNG, and JPEG. GD must be compiled with php. For the following example, provide your own image please. Example: ========================= $config=array( "text" => "123456", "text_colors" => "0 0 0", // RGB Seperated by spaces "image_loc" => "coupon.jpg", "image_type" => "JPEG", // PNG and GIF Supported // Optional arguments; default is center area on image "x_pos" => "", "y_pos" => "", ); $graphic=new img_add_txt($config);


<?
    
class img_add_txt{
    
    
        
/*
            Created by Richard Sumilang
            http://www.richard-sumilang.com
            
            
            object img_add_txt($array);
            ------------------------------------
            This function adds text on top of an image
            
            Usage:
            ------------------------------------
            $config=array(
                        "text" => "Coupon: Here is your free coupon",
                        "text_colors" => "255 68 0", // RGB Seperated by spaces
                        "image_loc" => "example.jpg",
                        "image_type" => "JPEG", // PNG and GIF Supported
                        // Optional arguments; default is center area on image
                        "x_pos" => "",
                        "y_pos" => "",
                        
            );
            
            $graphic=new img_add_txt($config);
        */
        
        
function img_add_txt($config){
    
            
// header
            //header("Content-Type: image/gif");
            
$this->func_header($config['image_type']);
            
            
// set up image
            
$im ImageCreateFromJPEG($config['image_loc']);
            
            
// Set up text colors
            
$text_colors=explode(" "$config['text_colors']);
            
$text_color ImageColorAllocate($im$text_colors['0'], $text_colors['1'], $text_colors['2']);
            
            
// get font dimensiona
            
$font_height ImageFontHeight(3);
            
$font_width ImageFontWidth(3);
            
            
// get image dimensiona
            
$image_height ImageSY($im);
            
$image_width ImageSX($im);
            
            
// get string length
            
$length $font_width strlen($config['text']);
            
            
// set the x, y cords of where the text will be placed
            
if(empty($config['x_pos'])){
                
// calculate start coordinates for string
                
$image_center_x = ($image_width/2)-($length/2);
            }else{
                
$image_center_x $config['x_pos'];
            }
            if(empty(
$config['y_pos'])){
                
// calculate start coordinates for string
                
$image_center_y = ($image_height/2)-($font_height/2);
            }else{
                
$image_center_y $config['y_pos'];
            }
            
            
// write string
            
ImageString($im3$image_center_x$image_center_y$config['text'], $text_color);
            
            
// output to browser
            
$this->output_image($config['image_type'], $im);
        
        }
// End img_add_txt
        
        
        /*
            Output the correct header based
            on file type
        */
        
function func_header($var){
            
            switch(
$var){
                case 
"PNG":
                    
header("Content-Type: image/png");
                break;
                
                case 
"GIF":
                    
header("Content-Type: image/gif");
                break;
                
                case 
"JPEG":
                    
header("Content-Type: image/jpeg");
                break;
            }
            
        }
// End func_header
    
    
        /*
            Output the correct image type
            based on type
        */
        
function output_image($var$pointer){
            
            switch(
$var){
                case 
"PNG":
                    
ImagePNG($pointer);
                break;
                
                case 
"GIF":
                    
ImageGIF($pointer);
                break;
                
                case 
"JPEG":
                    
ImageJPEG($pointer);
                break;
            }
            
        } 
// End out output image
            
    
// End of class
?>


Usage Example


<?
    
/*
        Pull in all the requirements
    */
    
require_once("class.img_add_txt.php");

    
    
/*
        coupon image
    */
    
$config=array(
                
"text" => "Test",
                
"text_colors" => "0 0 0"// RGB Seperated by spaces
                
"image_loc" => "coupon.jpg",
                
"image_type" => "JPEG"// PNG and GIF Supported
                // Optional arguments; default is center area on image
                
"x_pos" => "",
                
"y_pos" => "",
                
    );
    
    
$graphic=new img_add_txt($config);
?>


Rate This Script





Search



This Category All Categories