Thursday, February 12, 2026
HomeLanguagesPHP | imagefill() Function

PHP | imagefill() Function

The imagefill() function is an inbuilt function in PHP which is used to fill the image with the given color. This function performs a flood fill starting at the given coordinate (top left is 0, 0) with the given color in the image.

Syntax:

bool imagefill( $image, $x, $y, $color )

Parameters:This function accepts four parameters as mentioned above and described below:

  • $image: It is returned by one of the image creation functions, such as imagecreatetruecolor(). It is used to create size of image.
  • $x: This parameter is used to set x-coordinate of starting point.
  • $y: This parameter is used to set y-coordinate of starting point.
  • $color: It sets the color of image. A color identifier created by imagecolorallocate() function.

Return Value: This function returns True on success of False on failure.

Below programs illustrate the imagefill() function in PHP:

Program 1:




<?php
  
// Create an image of given size
$image = imagecreatetruecolor(500, 400);
  
// Sets background to green
$green = imagecolorallocate($image, 0, 153, 0);
imagefill($image, 0, 0, $green);
  
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>


Output:
image filled

Program 2:




<?php
    
// It create the size of image or blank image.
$image = imagecreatetruecolor(500, 300);
    
// Set the background color of image.
$bg = imagecolorallocate($image, 205, 220, 200);
    
// Fill background with above selected color.
imagefill($image, 0, 0, $bg);
   
// Set the color of an ellipse.
$col_ellipse = imagecolorallocate($image, 0, 102, 0);
    
// Function to draw the filled ellipse.
imagefilledellipse($image, 250, 150, 400, 250, $col_ellipse);
    
// Output of the image.
header("Content-type: image/png");
imagepng($image);
    
?>


Output:
image filled

Related Articles:

Reference: http://php.net/manual/en/function.imagefill.php

RELATED ARTICLES

Most Popular

Dominic
32497 POSTS0 COMMENTS
Milvus
128 POSTS0 COMMENTS
Nango Kala
6869 POSTS0 COMMENTS
Nicole Veronica
11996 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12090 POSTS0 COMMENTS
Shaida Kate Naidoo
7009 POSTS0 COMMENTS
Ted Musemwa
7246 POSTS0 COMMENTS
Thapelo Manthata
6958 POSTS0 COMMENTS
Umr Jansen
6947 POSTS0 COMMENTS