How to Add Text in an Image

This small script describes how to add a custom text to an image loaded onto your page. One of my client wanted to add a copyright notice in her images related to her site. So, I came up with this small PHP script for her.

Adding Text to an Image

Using the PHP GD function called imagecreatefromjpeg, imagecreatefromgif and imagecreatefrompng. We can add a custom text to an already existing image file.
So you have imagecreatefromjpeg for jpeg file, imagecreatefrompng for png file and so on.

< ?php
header("Content-type: image/png");
$string = "copyright: some text";
$font = 4;
$width = imagefontwidth($font)* strlen($string) ;
$height = imagefontheight($font) ;

This will setup the structure of the data being sent to the image file as well as what location to place the text at. In this case we want to place it on the bottom right of the page.

Now, we use imagecreatefrompng to create image from a file, set the text color, get the images dimensions, and figure on where to place the text.

$im = imagecreatefrompng("/path/to/yourimagefile");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
 
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);   //white background
$textColor = imagecolorallocate ($im, 0, 0, 0);   //black text
imagestring ($im, $font, $x, $y,  $string, $textColor);

Finally we produce the new image that will display custom text in an image.

imagepng ($im);
?>

Here is the complete code:

< ?php
header ("Content-type: image/png");
$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("/path/to/yourimagefile");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y,  $string, $textColor);
imagepng($im);
?>

You may like to view demo.

27 replys to How to Add Text in an Image

  1. You cannot place any rendering code in this file as it’s returning just an image file.

    No session_start, echo or just plain HTML.

  2. Modify
    $x = imagesx($im) – $width ;
    $y = imagesy($im) – $height;
    to put the text wherever you want
    Make sure you write the file path correctly.

  3. Thanks for this excellent tutorial!
    Contrary to what others might say, it worked excellently for me!
    You saved the day!

  4. It works, you just have to get rid of the space in the first line so it knows that it’s PHP.

    So change “< ?php" to "<?php"

  5. heres a hint after i spent hours trying to get it work also.
    the above code place into a new file .name it whatever you want .example image.php

    then use this on the page where you want to display the image
    echo “”;

    it worked for me after hours of scratching my head.

  6. Thanks.

    Got problems in beginning, but fixed and working flawlessly.

    Here is my code if anyone wants to use.

    <?php
    header ("Content-type: image/png");
    $string = "YOUR TEXT"; //YOUR TEXT eg: Hi Mom :)
    $imageO = "IMAGE.png"; //YOUR IMAGE AND LOCATION eg: images/myimage.png
    $imageN = "IMAGE2.png"; //YOUR NEW IMAGE eg: mynewimage.png

    //PLEASE NOTE IF YOU HAVE TROUBLE MAKE SURE YOUR OUTPUT IMAGE IS NOT IN A SEPARATE FOLDER, IT WILL OUTPUT TO THE FOLDER OF THE .php FILE

    $font = 4; //YOUR FONT SIZE

    $width = imagefontwidth($font) * strlen($string) ;
    $height = imagefontheight($font) ;
    $im = imagecreatefrompng($imageO);

    $x = imagesx($im) / 2; //PLACEMENT CENTERISH – X
    $y = imagesy($im) / 2; //PLACEMENT CENTERISH – Y

    $backgroundColor = imagecolorallocate ($im, 255, 255, 255);
    $textColor = imagecolorallocate ($im, 0, 0,0);
    imagestring ($im, $font, $x, $y, $string, $textColor);
    imagepng($im,$imageN);
    $w = imagesx($im);
    $h = imagesy($im);
    //PRINT IMAGE ON SCREEN
    echo "”;
    echo “M.R. Inc – 2012“;
    ?>

    Regards
    Mitchell

  7. Pingback: Anonymous
  8. Tnx for the code man…. It worked well but the maximum font size i can select is 5. Is there any way to increase the font size beyond 5…??

    Tnx in advance :)

  9. @ Ajay

    Can be 1, 2, 3, 4, 5 for built-in fonts in latin2 encoding (where higher numbers corresponding to larger fonts) or any of your own font identifiers registered with imageloadfont().

  10. Need it to just display the image it just created?

    <?php
    $string = "YOUR TEXT"; //YOUR TEXT eg: Hi Mom :)
    $imageO = "img/original-image.png"; //YOUR IMAGE AND LOCATION
    $imageN = "new-images/image.png"; //YOUR NEW IMAGE

    $font = 4; //YOUR FONT SIZE
    $width = imagefontwidth($font) * strlen($string) ;
    $height = imagefontheight($font) ;
    $im = imagecreatefrompng($imageO);
    $x = imagesx($im) / 2; //PLACEMENT CENTERISH – X
    $y = imagesy($im) / 2; //PLACEMENT CENTERISH – Y
    $backgroundColor = imagecolorallocate ($im, 255, 255, 255);
    $textColor = imagecolorallocate ($im, 0, 0,0);
    imagestring ($im, $font, $x, $y, $string, $textColor);
    imagepng($im,$imageN);
    $w = imagesx($im);
    $h = imagesy($im);

    //PRINT IMAGE ON SCREEN
    echo '’;
    ?>

Leave a Reply

Your email address will not be published. Required fields are marked *