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



/*** 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 ***/

 * @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 ***/
    /*** 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);

    /*** 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 =)


7 thoughts on “PHP – Add Text on an Image”

  1. 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


  2. sorry for the late response

    actualy image is being uploaded in a folder


    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);
    imagestring ($image,$font,0,0,$string,$black);

    imagegif ($image);


    1. have you tried just simply display an url image?

      header("Content-type: image/gif");

      is that ok?

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


  3. 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



Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.