More Shapes

Using three new functions, we can make a much more complicated image. These are: imagecreatetruecolor(), imagefilledellipse(), and imagefilledarc().

Here is a script using these new functions:

    header("content-type: image/png");

    $image = imagecreatetruecolor(400,300);
    $blue = imagecolorallocate($image, 0, 0, 255);
    $green = imagecolorallocate($image, 0, 255, 0);
    $red = imagecolorallocate($image, 255, 0, 0);

    imagefilledellipse($image, 200, 150, 200, 200, $red);
    imagefilledellipse($image, 200, 150, 180, 180, $blue);
    imagefilledellipse($image, 200, 150, 50, 50, $red);
    imagefilledarc($image, 200, 150, 200, 200, 345, 15, $green, IMG_ARC_PIE);
    imagefilledarc($image, 200, 150, 200, 200, 255, 285, $green, IMG_ARC_PIE);
    imagefilledarc($image, 200, 150, 200, 200, 165, 195, $green, IMG_ARC_PIE);
    imagefilledarc($image, 200, 150, 200, 200, 75, 105, $green, IMG_ARC_PIE);

    imagepng($image);
    imagedestroy($image);

The output from that script is shown in Figure 16-3.

Ellipses and circles

Figure 16-3. Ellipses and circles

Using imagecreatetruecolor() is the same as imagecreate()—it takes the same two parameters, and returns an image resource that is freed using imagedestroy(). The difference between the two is that imagecreatetruecolor() returns an image with a true-color palette, whereas an image made by imagecreate() cannot contain more than 256 colors. Furthermore, the image resource returned by imagecreatetruecolor() ...

Get PHP in a Nutshell 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.