This article will tell you how to write text on an existing PNG image using PHP using PHP's image manipulation functions. The image functions require the GD library to be installed. This article is fairly basic, however some more detail on the basic functions used is included in the article, "write text on a dynamically generated image in PHP"

First, we need the source image. In this example, we're using an image called image.png (with dimension 150x150, though this code will work with any size of image) which is shown below:
Sample Image
This image is going to be kept in the same directory on the webserver as the php file we're using to manipulate it.

Now it's time for PHP code to load the image. We make use of the function imagecreatefrompng() to get the image into PHP to manipulate. This takes the path to the image as a parameter. If the image can't be loaded then we'll tell it to die() and stop interpreting the page, however there are better ways of dealing with this error.

PHP:
  1. $im = imagecreatefrompng("image.png");
  2. // if there's an error, stop processing the page:
  3. if(!$im)
  4. {
  5.  die("");
  6. }

Next, we'll define some colours for use in the image using the imagecolorallocate() function:

PHP:
  1. $red_background = imagecolorallocate($im, 255, 0, 0);
  2. $black = imagecolorallocate($im, 0, 0, 0);

Now we get the dimensions of the image, using the imagesx() and imagesy(), which take the image resource as a parameter and return the width and height respectively:

PHP:
  1. // get the width and the height of the image
  2. $width = imagesx($im);
  3. $height = imagesy($im);

The next step is to draw a black rectangle on to the image to ensure that the text we write is visible. This step is optional, as you could write the text directly on to the image.
The coordinate system used in PHP is that the top left of an image is (0,0) and the bottom right is ($width, $height). We're going to draw the rectangle across the bottom 20px of the image, so we pass imagefilledrectangle two sets of coordinates: (0, $height-20) and ($width, $height). The coordinates we're passing - as the 2nd to 4th parameters) tell the method to draw a rectangle from a point on the left hand side, 20 pixels from the bottom, to the very bottom right of the image. The rectangle is then automatically filled black.

PHP:
  1. // draw a black rectangle across the bottom, say, 20 pixels of the image:
  2. imagefilledrectangle($im, 0, ($height-20) , $width, $height, $black);

Now it's time to write the text on the image. In this example, we will write the text in the middle of the rectangle we just created. First we declare two variables, $font and $text, to store the int representing the system font we're using and the text we're going to write to the image, respectively.

PHP:
  1. $font = 4; // store the int ID of the system font we're using in $font
  2. $text = "vdhri.net"; // store the text we're going to write in $text

Then we will calculate where the left edge of the text is. This is done by using imagefontwidth() to get the width of the system font we're using (which is fixed width, so all characters have the same width), then multiplying that by the number of characters in the string we're writing. This finds the length of the text in pixels. We then subtract that from the total width of the image and divide by two to find the left position, which we store in the variable $leftTextPos:

PHP:
  1. // calculate the left position of the text:
  2. $leftTextPos = ( $width - imagefontwidth($font)*strlen($text) )/2;

Now we write the text to the image, using the imagestring() function passing the variables we created earlier as parameters (NB. As height we're using the image height minus 18 pixels, to estimate a decent fit in the box, rather than calculating the height in the same way as width.)

PHP:
  1. // finally, write the string:
  2. imagestring($im, $font, $leftTextPos, $height-18, $text, $yellow);

Lastly, we send the image to the browser then clear it from memory:

PHP:
  1. // output the image
  2. // tell the browser what we're sending it
  3. Header('Content-type: image/png');
  4. // output the image as a png
  5. imagepng($im);

That's it! the output image is:
Output image
This will be shown in the browser whenever anyone accesses the php page we've just created!

To finish off, here's the code in full:

PHP:
  1. <?
  2. // load the image from the file specified:
  3. $im = imagecreatefrompng("image.png");
  4. // if there's an error, stop processing the page:
  5. if(!$im)
  6. {
  7.  die("");
  8. }
  9.  
  10. // define some colours to use with the image
  11. $yellow = imagecolorallocate($im, 255, 255, 0);
  12. $black = imagecolorallocate($im, 0, 0, 0);
  13.  
  14. // get the width and the height of the image
  15. $width = imagesx($im);
  16. $height = imagesy($im);
  17.  
  18. // draw a black rectangle across the bottom, say, 20 pixels of the image:
  19. imagefilledrectangle($im, 0, ($height-20) , $width, $height, $black);
  20.  
  21. // now we want to write in the centre of the rectangle:
  22. $font = 4; // store the int ID of the system font we're using in $font
  23. $text = "vdhri.net"; // store the text we're going to write in $text
  24. // calculate the left position of the text:
  25. $leftTextPos = ( $width - imagefontwidth($font)*strlen($text) )/2;
  26. // finally, write the string:
  27. imagestring($im, $font, $leftTextPos, $height-18, $text, $yellow);
  28.  
  29. // output the image
  30. // tell the browser what we're sending it
  31. Header('Content-type: image/png');
  32. // output the image as a png
  33. imagepng($im);
  34.  
  35. // tidy up
  36. imagedestroy($im);
  37. ?>

If you have a low-traffic website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!


9 Responses to “Write Text on to an existing PNG image using PHP”  

  1. Gravatar Icon 1 Sofia

    Thanks! Just what i needed :)

  2. Gravatar Icon 2 Domino

    Cool, but how to make it split into 2 lines if it overflows?

  3. Gravatar Icon 3 Noutrious

    Please show me an example, how with
    $left $right $center $downleft $downright i could put the text in x, y place?

    Thanks

  4. Gravatar Icon 4 Sjaak Gerritse

    I like to use an existing image (gif, 200×200 pixels) in a new image (1024×768) which I will draw with imagepng.
    Can you please help me?
    Thanks

  5. Gravatar Icon 5 Mirek Soja

    Congratulation! Excellent tutorial!
    Thanks,
    Mirek

  6. Gravatar Icon 6 hassan

    thnks

  7. Gravatar Icon 7 Andy Bailey

    excellent.

    I’ve wanted to do this for a long time but all the texts on it have been way too complex, yours had me making my own dynamic button within minutes.

    a big thumbs up !!

  8. Gravatar Icon 8 Md. Mizanur Rahman

    How it possible to display multiple image from Mysql database?

  9. Gravatar Icon 9 Roshith kaniyamchalil

    Hi,

    Very good guidelines.very helpful.

    thanks
    Roshith Kaniyamchlil

Leave a Reply

You must log in to post a comment.


Categories