imagefill() function in PHP

The imagefill() function is used to perform a flood fill operation on an image, filling an area with a specified color starting from a given point.

Syntax

imagefill(img, x, y, color)

Parameters

  • img − An image resource created by functions like imagecreatetruecolor() or imagecreatefromjpeg()

  • x − x-coordinate of the starting point for the fill operation

  • y − y-coordinate of the starting point for the fill operation

  • color − The fill color identifier created with imagecolorallocate()

Return Value

The imagefill() function returns TRUE on success or FALSE on failure.

Example

The following example creates a blank canvas and fills it with a light green color ?

<?php
    // Create a 400x400 pixel canvas
    $img = imagecreatetruecolor(400, 400);
    
    // Allocate a light green color (RGB: 190, 255, 120)
    $color = imagecolorallocate($img, 190, 255, 120);
    
    // Fill the entire canvas starting from top-left corner (0,0)
    imagefill($img, 0, 0, $color);
    
    // Set the content type to PNG
    header('Content-type: image/png');
    
    // Output the image
    imagepng($img);
    
    // Clean up memory
    imagedestroy($img);
?>

Output

The above code generates a 400x400 pixel image filled with light green color. The output would be a solid colored rectangle displayed in the browser.

400x400 Light Green Fill

Key Points

  • The fill operation starts from the specified (x, y) coordinates and spreads to adjacent pixels of the same color
  • This function requires the GD extension to be enabled in PHP
  • Always use imagedestroy() to free up memory after image processing

Conclusion

The imagefill() function is essential for creating solid color backgrounds and performing flood fill operations in PHP image manipulation. It provides a simple way to fill areas with specified colors starting from any coordinate point.

Updated on: 2026-03-15T07:47:42+05:30

445 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements