In the PHP image processing function library, imagefilltoborder is a very practical function used to fill a specified area with color until it reaches a border color. This feature is common in image editing and dynamic graphics generation, especially when filling complex regions with color.
This article will introduce the usage and working principle of the imagefilltoborder function through specific examples, helping you quickly grasp this image processing skill.
The imagefilltoborder function fills color starting from a specified pixel coordinate and continues until it encounters a certain border color. Its function definition is as follows:
bool imagefilltoborder ( resource $image , int $x , int $y , int $border , int $color )
$image: Image resource handle.
$x, $y: Coordinates of the starting fill point.
$border: The index value of the border color; the fill will not cross this border color.
$color: The color index value used for filling.
The function returns true if the fill succeeds, otherwise it returns false.
Suppose you have an image with different colored areas and want to fill a specific region without the color spilling over the boundaries. The imagefilltoborder function can fill from the point you specify until it hits the preset border color.
The example below demonstrates how to create a rectangle with a black border and fill its interior with red using imagefilltoborder.
<?php
// Create a blank image
$width = 200;
$height = 100;
$image = imagecreatetruecolor($width, $height);
<p>// Allocate colors<br>
$white = imagecolorallocate($image, 255, 255, 255);<br>
$black = imagecolorallocate($image, 0, 0, 0);<br>
$red = imagecolorallocate($image, 255, 0, 0);</p>
<p>// Fill background with white<br>
imagefill($image, 0, 0, $white);</p>
<p>// Draw a black rectangle border<br>
imagerectangle($image, 50, 20, 150, 80, $black);</p>
<p>// Use imagefilltoborder to fill red from point (51,21) until the black border is reached<br>
imagefilltoborder($image, 51, 21, $black, $red);</p>
<p>// Output the image<br>
header("Content-Type: image/png");<br>
imagepng($image);</p>
<p>// Free resources<br>
imagedestroy($image);<br>
?><br>
First, create a true color image of size 200x100.
Set the background color to white.
Draw a black rectangular border.
Start filling inside the rectangle with red from a point inside it until the black border is encountered.
Output the image directly in PNG format through the browser.
After running this code, you will see a rectangle filled with red, bounded by a black border, with the fill color not exceeding the boundary.
imagefilltoborder will keep filling until it reaches the border color, so the border color must be precise; otherwise, the fill may exceed the expected range.
The border color and the fill color cannot be the same; otherwise, the fill will be ineffective.
The starting point coordinates must be inside the non-border area; otherwise, no fill effect will occur.
imagefilltoborder is an essential tool in PHP image processing for filling color within an area, especially suitable for scenarios where filling must be restricted by borders. By properly setting the start point and border color, you can achieve precise filling of complex image areas.
For more image processing functions, refer to the official PHP manual: https://gitbox.net/manual/en/function.imagefilltoborder.php