The function of imagefilltoborder() is to fill at a certain point in the image until the specified boundary color is encountered. This is similar to the behavior of paint bucket tools.
Function prototype:
bool imagefilltoborder(GdImage $image, int $x, int $y, int $border_color, int $color)
$x and $y are the starting point coordinates;
$border_color is the boundary color of the fill;
$color is the fill color.
The characteristic of this function is "boundary trigger fill". It starts from the specified coordinates and replaces all pixels that do not belong to the boundary color with a fill color until the boundary is encountered.
imagefilledrectangle() is used to draw a filled rectangle figure. This function is more inclined to the drawing of geometric figures.
Function prototype:
bool imagefilledrectangle(GdImage $image, int $x1, int $y1, int $x2, int $y2, int $color)
$x1, $y1 is the upper left corner coordinate;
$x2, $y2 are the coordinates of the lower right corner;
$color is the fill color.
This function directly fills all pixels in the specified rectangle area with the specified color, without considering the boundary color and does not depend on the context of the image content.
Function | imagefilltoborder | imagefilledrectangle |
---|---|---|
Starting point | Requires a starting point | Specify coordinate area |
Filling conditions | Stop when encountering the border color | Fill by coordinate rectangle only |
flexibility | High, can be used for irregular graphics | Low, suitable for regular rectangles |
performance | Relatively slow (recursive search) | High efficiency |
Application scenarios | Similar to the "paint bucket" tool for boundary filling areas | Scenes with clear structures such as drawing and generating charts |
Give an example:
Suppose you are creating an interactive map that requires the user to click on any area and fill in the color, then imagefilltoborder() is a more suitable choice because you only need to pass in the click position and boundary color.
$image = imagecreatefrompng("https://gitbox.net/images/map.png");
$fill_color = imagecolorallocate($image, 255, 0, 0);
$border_color = imagecolorallocate($image, 0, 0, 0);
imagefilltoborder($image, $x, $y, $border_color, $fill_color);
But if you want to draw a rectangular bar in the chart, such as a column in the bar chart, then imagefilledrectangle() is a simpler and more straightforward choice:
$image = imagecreatetruecolor(200, 100);
$color = imagecolorallocate($image, 0, 128, 255);
imagefilledrectangle($image, 20, 20, 80, 80, $color);
This question does not have an absolute answer, but depends on your specific needs:
You need to fill irregular areas from the point (such as filling the area inside the outline after clicking) : select imagefilltoborder() .
It is necessary to quickly draw regular geometric shapes (such as chart elements, background blocks) : using imagefilledrectangle() is more appropriate.
If you are worried about performance issues and the shape of the filler graph is certain, then imagefilledrectangle() is preferred. It executes faster and there are no unexpected boundary detection problems.