O'Reilly logo

Advanced PHP for Web Professionals by Christopher Cosentino

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required