This tutorial will tell you how to write text to a blank dynamically generated PNG image in PHP. This is fairly simple, so it will just be the first step in a series of tutorials dealing with images in PHP. The image functions require the GD library to be installed.

The code we're using and the output is below, and below that is an explanation of what the code does.

PHP:
  1. <?
  2. // create an image with width 100px, height 20px
  3. $image = imagecreate(100, 20);
  4.  
  5. // create a red colour for the background
  6. // as it is the first call to imagecolorallocate(), this fills the background automatically
  7. $red_background = imagecolorallocate($image, 255, 0, 0);
  8. // create a black colour for writing on the image
  9. $black = imagecolorallocate($image, 0, 0, 0);
  10.  
  11. // write the string "vdhri.net" on the image
  12. // the top left of the text is at the position (10, 2)
  13. imagestring($image, 4, 10, 2, ‘vdhri.net’, $black);
  14.  
  15. // output the image
  16. // tell the browser what we’re sending it
  17. Header(’Content-type: image/png’);
  18. // output the image as a png
  19. imagepng($image);
  20.  
  21. // tidy up
  22. imagedestroy($image);
  23. ?>

Output:
Output image

The first step is to tell PHP to create the image, we do this with the imagecreate() function, which takes the desired width and height as parameters. This returns the image identifier, which we store in the variable $image.

Next, we use the imagecolorallocate() function to define some colours for use in our image. This takes as parameters, the image identifier we're using, then the red, green and blue values for the colour. The first time we call imagecolorallocate() it fills in the background of the image with that colour.

We then use the imagestring() function to write text on the image. This function takes six parameters: the image identifier, an integer 1-5 determining which built in font to use (we're choosing 4), x and y coordinates giving the top left position of the text, the text to write, and the colour to write it in.

It's then time to output the image.
First, we need to tell the browser what type of image we're sending (this is normally text, which wouldn't work for images), we do this with the line:
Header('Content-type: image/png');

Next, we need to tell PHP to output the image as a PNG, this is done with the imagepng() function, which takes the image identifier as a parameter.

That's it! The last thing we do is to use the imagedestroy() function to delete the image from memory, it takes the image identifier as a parameter.

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!


6 Responses to “Write text on a dynamically generated image using PHP”  

  1. Gravatar Icon 1 Azhar

    Hi there,
    I would like to know if someone can help in writing a script like one used at:
    http://www.snugglepie.com
    http://www.tickerfactory.com

  2. Gravatar Icon 2 navneet

    Hi,
    problem: japanese text to Image conversion problem.
    when i tried to convert japanese charcter to image then its displaying nothing.
    plz help……….

  3. Gravatar Icon 3 Mgccl

    To write Japanese or any other language on GD image, you have to get the right font, use imagettftext function.

  4. Gravatar Icon 4 rach

    can u help me in creating image verification system kind of image which is used to check whether data is entered by a human being

  5. Gravatar Icon 5 Muhammad Danish Khan

    Hello guys,

    i need to know that how i create an image with two images like mosaic view (Create an image with two images showing) i also need to know that how can i make hyper link or tool tip for every Image in mosaic

    thanx,

    waiting for soon reply.

  6. Gravatar Icon 6 Hemal Jivani

    Hi I would like to know how can I incerease th font of the custom text I am writing in can you help me please it will be greatful.

    Thanks in advance. Any scripts that is ready will be appreciable.

    Thanks very much

    Regards
    Hemal Jivani

Leave a Reply

You must log in to post a comment.


Categories