PHP – Add Text on an Image

I find a very simple method on web about adding text on an image by PHP.
Reference: Write Text To Image

What u need is the following 3 files and an web server.

  • msjh.ttf – The font file for the text
  • test.png – The original image
  • text_on_image.php – The php file which will display the text.png with text added

text_on_image.php

<?php

/*** set the header for the image ***/
header("Content-type: image/png");

/*** specify an image and text ***/
$im = writeToImage('test.png', 'HelloWorld');

/*** spit the image out the other end ***/
imagepng($im);

/**
 *
 * @Write text to an existing image
 *
 * @Author Kevin Waterson
 *
 * @access public
 *
 * @param string The image path
 *
 * @param string The text string
 *
 * @return resource
 *
 */
function writeToImage($imagefile, $text){
/*** make sure the file exists ***/
if(file_exists($imagefile))
    {    
    /*** create image ***/
    $im = @imagecreatefrompng($imagefile);

    /*** create the text color ***/
    $text_color = imagecolorallocate($im, 0, 255, 0);
	
    /*** set the font file ***/
    $font_file = './msjh.ttf';

    /*** splatter the image with text ***/
	imagefttext($im, 20, 0, 25, 150, $text_color, $font_file, $text);

    }
else
    {
    /*** if the file does not exist we will create our own image ***/
    /*** Create a black image ***/
    $im  = imagecreatetruecolor(150, 30); /* Create a black image */

    /*** the background color ***/
    $bgc = imagecolorallocate($im, 255, 255, 255);

    /*** the text color ***/
    $tc  = imagecolorallocate($im, 0, 0, 0);

    /*** a little rectangle ***/
    imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

    /*** output and error message ***/
    imagestring($im, 1, 5, 5, "Error loading $imagefile", $tc);
    }
return $im;
}
?>

You can add text to gif or jpeg by using the imagegif()/imagejpeg() and imagecreatefromgi()/imagecreatefromjpeg() to replace the imagepng() and imagecreatefrompng().

Done =)

About these ads

7 thoughts on “PHP – Add Text on an Image

  1. Ashwini Kumar

    Its nice and working for all types of images.
    but what about the case in which i have to write text
    on an image (with out creating the image ) that is image is physicaly available ,

    thanx in advance
    Ashwini

    Reply
  2. Ashwini Kumar

    sorry for the late response

    actualy image is being uploaded in a folder

    http://server1\PHP\test_ashwini

    but it is not showing image

    here is the code

    header(“Content-type: image/gif”);

    $string = “This is my test string.”;

    //$font = 2;
    //$width = imagefontwidth($font) * strlen($string);
    //$height = imagefontheight($font);

    $image = imagecreatefromgif(“http://server1/PHP/test_ashwini/logo.gif”);
    //$image = imagecreatetruecolor ($width,$height);
    $white = imagecolorallocate ($image,255,100,255);
    $black = imagecolorallocate ($image,0,0,0);
    imagefill($image,0,0,$white);
    imagestring ($image,$font,0,0,$string,$black);

    imagegif ($image);
    imagedestroy($image);

    Reply
    1. ykyuen Post author

      have you tried just simply display an url image?

      header("Content-type: image/gif");
      imagegif(imagecreatefromgif('http://url/test.gif'));
      

      is that ok?

      by the way, u didn’t use the imagefttext() to write the text on the image.

      Reply
  3. Ashwini Kumar

    hello friend

    i am facing other problem

    while scling the image (more than 100% mean increasing the width and height of the image ) imahe remains same but a black background appears in extra dimension.

    while scaling to less thn hundred it works fine

    any help

    thanx

    Reply
  4. Pingback: PHP – Add text on an image with background color and save it on the server | Eureka!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s