How to write text on an image in php

To write text on an image in php you need to make sure that your PHP interpreter must have been compiled with the GD library and True Type Font support(if you want to use True Type Fonts for writing text) compiled into it as well.

Follow these steps to write text on an image using PHP-

1.Load the image-PHP has three library function to load the image –

I)imagecreatefromgif ( )- for gif images

II)imagecreatefromjpeg ( ) -for jpeg images

III)imagecreatefrompng() -for png  images

If you are going to use an jpeg image then write the following code-

 2.Define some colors –Now you need to load some colors into variables.

 3.Get the dimensions of image-Get the width and height of the image  it will help you to determine from where to start writing.

 4.Assign font size, text and position –Choose the size of font ,the text which will display on the image and the position of the text relative to image.

 5.Write the text on image-Now write the text on the image.

 6.Set the Content-type header-It will tell the browser that we are sending an jpeg image.

 7.Output the image and free resources-The last step is to output  an image and free the resources.

After completing  above 7 steps your code will look like this.

 Download Source


  • Awesome Post. Lighting is most beautiful.
    Thanks for the Post.