Tuesday, May 19, 2009

Create Dynamic Images with Text Using PHP GD

First check weather php gd library is enabled.


echo '<pre>';


echo '</pre>';


If you get a error or a blank page that means the gd is not enabled in your server.
Contact your hosting and ask for php gd to be enabled.

Try following coding, everything is commented.


//save the arial.ttf font file in the same folder
$font = 'arial.ttf';
$fontsize = 10;

$YourText = 'Hi This Work';
$Text_Box = imagettfbbox($fontsize, 0, $font, $YourText);

//create image width 250, height 100
$image = imagecreatetruecolor(250,100);

//0,0,0 is black
$bgcolor = imagecolorallocate($image, 0, 0, 0);
//255, 255, 255 is white
$fontcolor = imagecolorallocate($image, 255, 255, 255);

//create a rectangle with the back ground coloe
imagefilledrectangle($image, 0, 0, 250, 100, $bgcolor);

$x = 10;
$y = 20;
//draw text on image
imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, $font, $YourText);

//setting png headers
header('Content-type: image/png');
//create png image
//delete tempory files


You can use this php file in a html file like this also

<img src="your_file_name.php" />


  1. "the image can't be displayed because it contains error"
    Messege is displaying

  2. i want to make a dynamic picture and should be upload it into remote server if you can please share with me the code .....tghanks a lot

  3. i want to craete a dynamic image and it should upload into a remote server if you can please share me your code