Color and Image Fills

The function imagefill() takes four parameters: an image resource, the X and Y coordinates to start the fill at, and the color with which to fill. The fill will automatically flood your image with color outward from the point specified by your X and Y parameters until it encounters any other color.

Put this imagefill() function call into your addingtext.php script, just after imagettftext():

    $red = imagecolorallocate($image, 255, 0, 0);
    imagefill($image, 0, 0, $red);

With that function, our red color is used to fill in the image starting from (0,0), which is the top-left corner. If you load the script into your web browser, you will see the fill has left some parts of the blue behind—the parts it couldn't "reach" inside the text. Also, you will notice there is a bluish fringe around the text, where the white text was anti-aliased (smoothed) against the blue background, producing a blue-white edge to the text. Figure 16-12 shows how the fill looks with the blue areas that could not be reached inside letters. Figure 16-13 shows a close-up of the letter "o," where you can see the anti-aliasing in action. As our fill starts on blue, it will not fill over any other shade of blue, which is why this fringe has been left there.

Our first fill leaves blue areas inside letters, and also a blue fringe around each of the letters

Figure 16-12. Our first fill leaves blue areas inside letters, and also a blue fringe around each of the letters

Figure 16-13. Anti-aliasing ...

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.