Using Brushes

In the same way that imagesettile() allows you to use a picture for filling, imagesetbrush() allows you to use a picture for an outline. While this could be a premade picture you've just loaded, you can get nice effects by using handmade pictures that are swept around basic shapes.

Figure 16-14 shows a picture of a lot of dots ranging in color from red to yellow—not very interesting, but great for using as a brush.

The picture we'll be using as our brush

Figure 16-14. The picture we'll be using as our brush

Those dots were created with this script:

    $brush = imagecreate(100,100);

    $brushtrans = imagecolorallocate($brush, 0, 0, 0);
    imagecolortransparent($brush, $brushtrans);

    for ($k = 1; $k < 18; ++$k) {
            $color = imagecolorallocate($brush, 255, $k * 15, 0);
            imagefilledellipse($brush, $k * 5, $k * 5, 5, 5, $color);
    }

    imagepng($brush);
    imagedestroy($brush);

The next step is to create a larger image, recreate that brush, and use it as the outline for a shape. Here's the code:

 $pic = imagecreatetruecolor(600,600); $brush = imagecreate(100,100); $brushtrans = imagecolorallocate($brush, 0, 0, 0); imagecolortransparent($brush, $brushtrans); for ($k = 1; $k < 18; ++$k) { $color = imagecolorallocate($brush, 255, $k * 15, 0); imagefilledellipse($brush, $k * 5, $k * 5, 5, 5, $color); } imagesetbrush($pic, $brush); imageellipse($pic, 300, 300, 350, 350, IMG_COLOR_BRUSHED); imagepng($pic); imagedestroy($pic); imagedestroy($brush); ...

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.