int imagefill ( resource image, int x, int y, int fill_color)
int imagefilltoborder ( resource image, int x, int y, int border_color, int fill_color)
int imagesettile ( resource image, resource tile)
You'll be glad to hear that compared to calculating bounding boxes for text, using fills is remarkably easy. The function imagefill() takes just four parameters: an image resource, the X and Y coordinates to start the fill at, and the colour with which to fill. The fill will automatically flood your image with colour outwards from the point specified by your X and Y parameters until it encounters any other colour.
Putting this imagefill() function call into your addingtext.php script, just after imagettftext():
With that function, our red colour 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 blue-ish fringe around the text, where the white text was anti-aliased (smoothed) against the blue background, producing a blue-white edge to the text. Shows how the fill looks, with the blue areas that could not be reached inside letters, and 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.
There is another similar function, imagefilltoborder(), where the colour to fill is the fifth parameter, and the new fourth parameter is the colour at which the fill should stop "flowing". That is, the fill will keep flooding outwards until it hits the border colour. If we change our imagefill() call to imagefilltoborder() and specify $white as the colour at which to stop, it should eliminate the anti-aliasing fringe around the letters. Replace the imagefill() call with this:
Whereas the imagefill() function will fill the image with colour until it encounters any other colour, the imagefilltoborder() function call shown above will fill the image with colour and not stop till it finds pixels coloured with $white. When you look at it in your browser, you will notice the text has become very jagged because our red fill has taken away all the blue-white smoothing.
There is one last neat effect you can do with fills, and for that we need the imagesettile() function. What this allows you to do is use an existing image as the picture for your fill in place of a colour, which PHP will tile across your image as it fills. This function takes just two parameters: the image you want to change, and the image to use as a tile fill.
In order to use a tiled image for your fills rather than a colour, you simply pass the special constant IMG_COLOR_TILED where you would usually pass a colour. Thus, we can alter the addingtext.php script to look like this:
You can use imagesettile() as many times as you need in order to do several fills using different images. As an added bonus, once you have used imagesettile() you can also use IMG_COLOR_TILED wherever you create filled shapes - just use it in place of the colour and you can create tiled polygons, ellipses, etc.