Приглашаем посетить
Русский язык (rus-yaz.niv.ru)

Section 16.8.  Color and Image Fills

Previous
Table of Contents
Next

16.8. 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 behindthe 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.

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


Figure 16-13. Anti-aliasing has made PHP blend the blue and white together on the edges of the letters to get a smooth effectour fill leaves these intact
Section 16.8.  Color and Image Fills


There is a similar function, imagefilltoborder( ), where the color to fill is the fifth parameter, and the new fourth parameter is the color at which the fill should stop "flowing." That is, the fill will keep flooding outward until it hits the border color. If we change our imagefill( ) call to imagefilltoborder( ) and specify $white as the color 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 color until it encounters any other color, the imagefilltoborder( ) function call shown above will fill the image with color and continue until it finds pixels colored 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.

The imagesettile( ) function allows you to use an existing image as the picture for your fill in place of a color, 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 color, pass the constant IMG_COLOR_TILED where you would usually pass a color. 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");

    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 shapesjust use it in place of the color and you can create tiled polygons, ellipses, and other shapes.


Previous
Table of Contents
Next