As mentioned above, the first thing we need to do is create a blank image canvas for PHP to work on – a call to the ImageCreate() function does just this. The only two things we need to tell it are the width and height of the image we wish to create. ImageCreate() will then return an identifier that identifies that blank image in memory. All the subsequent image functions we use will have to refer to the image in memory by means of this identifier.
This identifier is similar to a file handle or database link ID, as we've used in recent chapters, but refers to the location of our image canvas in memory rather than an open file on the disk or an active database connection.
$image = ImageCreate(200,150);
We've now defined $image as the identifier referring to
the new, blank canvas (200 pixels wide by 150 pixels high) that ImageCreate() has just created for us. Now that this 200 x 150 pixel image is in memory we can start drawing on it. First though, we need to know what color we'll be drawing in.
Setting up Colors
Before we can tell PHP that we want to use a certain color on our canvas, we have to create that color. We do this in a similar fashion to that of creating the image canvas:
We've used the function ImageColorAllocate() to
define two colors for our image: a gray and a blue. Each color is assigned a unique identifier and tied to an existing image canvas. As you can see, this function requires 4 pieces of information:
The first is the identifier of the image canvas with which this color will be used. This is the same identifier that we saw returned to us earlier from the ImageCreate() function.
The second, third and fourth things we need to tell ImageColorAllocate() are the respective values of red, green and blue components for that color, which must lie between 0 and 255.
Computers make up color by mixing different quantities of Red, Green and Blue. This is what is known as RGB mode color. Each of red, green and blue can range from 0 to 255. Setting each of red, green and blue to 0 will give us black (total absence of color), a value of 255 for all three will give us white. If we want to stick to web-safe colors, then we have to limit our values of red, green and blue to multiples of 51. That gives us 6 possible values for each of red, green and blue, for a total of 216 colors. $gray and $blue are now two new identifiers to the colors gray and blue. We created the former by specifying equal values of each color. Note that 204 was not a number we pulled from the hat, but a multiple of 51, and we've therefore defined a web-safe color. We defined $blue by telling ImageColorAllocate() that the red component was 0, the green component was 0 and that the blue component was 255.
The Image Coordinate System
While we're dealing with theory here, we may as well take a look at how the image coordinate system works. It may sound like a mouthful but it's simply a way for us to precisely describe points in the image.
If you're familiar with creating graphs, you'll probably be used to the x and y values radiating outwards from the bottom left hand corner. In PHP, all coordinates radiate outwards from the top left-hand corner of the image, as the image below shows:
With this in mind, let's take a look at how our blank $image is laid out:
The x-y coordinates for the top left corner of $image are 0,0. This will be true for every image that you create. The x-coordinates extend to the right for 200 pixels and y-coordinates extend down for 150 pixels, so the bottom right-hand corner of the image has x-y coordinates of 199,149.