Generating Graphics with GD

Once you have configured PHP to use the GD library, you can start creating some graphics.

The Lines and Text

The base function to use when creating images using the GD library is the ImageCreate() function.

$length = 300; 
$width = 100;
$image = ImageCreate($length, $width);

Once you have created an image, you need to add some color to it using the ImageColorAllocate() function. ImageColorAllaocate() takes as its arguments the image you are creating and an RGB color value:

$blue = ImageColorAllocate($image, 0, 0, 255); 

So now you have created a box and given it a color. Now all that is left is to display the image in a browser. To do that, you need two more lines of code:

 Header("Content-type: image/png"); ImagePNG($image); ...

Get Advanced PHP for Web Professionals now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.