Flood fill to specific color in PHP using imagefilltoborder() (GD) function.

The imagefilltoborder() function is a PHP GD library function that performs flood fill with a specific color, filling a region until it reaches a defined border color. The fill starts from a specified point (x, y) and spreads until it encounters the border color.

Syntax

bool imagefilltoborder(resource $image, int $x, int $y, int $border, int $color)

Parameters

The imagefilltoborder() function accepts five parameters ?

  • $image − The image resource created by image creation functions

  • $x − X-coordinate of the starting point for the fill

  • $y − Y-coordinate of the starting point for the fill

  • $border − The border color that stops the fill (color identifier)

  • $color − The fill color (color identifier)

Return Value

Returns true on success or false on failure.

Example 1: Basic Flood Fill

This example creates a simple rectangle and fills it with color ?

<?php
    // Create a blank image
    $img = imagecreatetruecolor(300, 200);
    
    // Define colors
    $white = imagecolorallocate($img, 255, 255, 255);
    $black = imagecolorallocate($img, 0, 0, 0);
    $red = imagecolorallocate($img, 255, 0, 0);
    
    // Fill background with white
    imagefill($img, 0, 0, $white);
    
    // Draw a black rectangle border
    imagerectangle($img, 50, 50, 250, 150, $black);
    
    // Fill the rectangle with red color
    imagefilltoborder($img, 150, 100, $black, $red);
    
    // Output the image
    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);
?>

Example 2: Fill an Ellipse

This example demonstrates filling an ellipse shape with color ?

<?php
    // Create image with gray background
    $img = imagecreatetruecolor(400, 300);
    $gray = imagecolorallocate($img, 128, 128, 128);
    imagefill($img, 0, 0, $gray);
    
    // Draw an ellipse with black border
    $black = imagecolorallocate($img, 0, 0, 0);
    imageellipse($img, 200, 150, 200, 150, $black);
    
    // Define fill color
    $blue = imagecolorallocate($img, 0, 100, 255);
    
    // Fill the ellipse interior
    imagefilltoborder($img, 200, 150, $black, $blue);
    
    // Output the image
    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);
?>

Key Points

  • The fill spreads from the starting point until it encounters the border color

  • If no border color is found, the fill may spread across the entire image

  • The border color must exactly match the specified color value

  • Always use imagedestroy() to free memory after processing

Conclusion

The imagefilltoborder() function is useful for flood-filling enclosed areas with specific colors. It works by spreading fill color from a starting point until it encounters the specified border color, making it ideal for filling shapes and regions in image processing applications.

Updated on: 2026-03-15T09:47:07+05:30

452 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements