Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia > Images Wish List | Report Bug | About Me ]

11.2.8     Colour and image fills: imagefill(), imagefilltoborder(), and imagesettile()

This is NOT the latest copy of this book; click here for the latest version.

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:

<?php
    
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");

    
imagepng($image);
    
imagedestroy($image);
    
imagedestroy($bg);
?>

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.





<< 11.2.7 Loading existing images: imagecreatefrompng(), imagecreatefromjpeg(), and getimagesize()   11.2.9 Adding transparency: imagecolortransparent() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow