05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

many colors as you need. The latter are known as true color formats, because 24-bit<br />

color (8 bits for each of red, green, and blue) gives more hues than the human eye<br />

can distinguish.<br />

Creating and Drawing Images<br />

For now, let’s start with the simplest possible GD example. Example 9-1 is a script<br />

that generates a black filled square. The code works with any version of GD that supports<br />

the PNG image format.<br />

Example 9-1. A black square on a white background (black.php)<br />

<br />

Example 9-1 illustrates the basic steps in generating any image: creating the image,<br />

allocating colors, drawing the image, and then saving or sending the image.<br />

Figure 9-1 shows the output of Example 9-1.<br />

Figure 9-1. A black square on a white background<br />

To see the result, simply point your browser at the black.php <strong>PHP</strong> page. To embed<br />

this image in a web page, use:<br />

<br />

The Structure of a Graphics Program<br />

Most dynamic image-generation programs followthe same basic steps outlined in<br />

Example 9-1.<br />

You can create a 256-color image with the ImageCreate( ) function, which returns an<br />

image handle:<br />

$image = ImageCreate(width, height);<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Creating and Drawing Images | 217

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!