Colour and image fills

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():

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

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:

imagefilltoborder($image, 0, 0, $white, $red);

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:

    if(!isset($_GET['size'])) $_GET['size'] = 44;
    if(!isset($_GET['text'])) $_GET['text'] = "Hello, world!";
    $size = imagettfbbox($_GET['size'], 0, "ARIAL", $_GET['text']);
    $xsize = abs($size[0]) + abs($size[2]);
    $ysize = abs($size[5]) + abs($size[1]);

    $image = imagecreate($xsize, $ysize);
    $blue = imagecolorallocate($image, 0, 0, 255);
    $white = ImageColorAllocate($image, 255,255,255);
    imagettftext($image, $_GET['size'], 0, abs($size[0]), $ysize, $white, "ARIAL", $_GET['text']);

    $bg = imagecreatefrompng("button_mini.png");
    imagesettile($image, $bg);
    imagefill($image, 0, 0, IMG_COLOR_TILED);
    header("content-type: image/png");


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.


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Adding transparency >>

Previous chapter: Loading existing images

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.