PHP 8.3.4 Released!

imagefilltoborder

(PHP 4, PHP 5, PHP 7, PHP 8)

imagefilltoborder漫水填充特定颜色

说明

imagefilltoborder(
    GdImage $image,
    int $x,
    int $y,
    int $border_color,
    int $color
): bool

imagefilltoborder() 执行漫水填充,其边框颜色由 border_color 定义。填充的起点是 x, y(左上角是0, 0),区域用颜色 color 填充。【注:边界内的所有颜色都会被填充。如果指定的边界色和该点颜色相同,则没有填充。如果图像中没有该边界色,则整幅图像都会被填充。】

参数

image

由图象创建函数(例如imagecreatetruecolor())返回的 GdImage 对象。

x

起点的 x 坐标。

y

起点的 y 坐标。

border_color

边框颜色。颜色标识符使用 imagecolorallocate() 创建。

color

填充颜色。颜色标识符使用 imagecolorallocate() 创建。

返回值

成功时返回 true, 或者在失败时返回 false

更新日志

版本 说明
8.0.0 image 现在需要 GdImage 实例;之前需要有效的 gd resource

示例

示例 #1 用颜色填充椭圆

<?php
// Create the image handle, set the background to white
$im = imagecreatetruecolor(100, 100);
imagefilledrectangle($im, 0, 0, 100, 100, imagecolorallocate($im, 255, 255, 255));

// Draw an ellipse to fill with a black border
imageellipse($im, 50, 50, 50, 50, imagecolorallocate($im, 0, 0, 0));

// Set the border and fill colors
$border = imagecolorallocate($im, 0, 0, 0);
$fill = imagecolorallocate($im, 255, 0, 0);

// Fill the selection
imagefilltoborder($im, 50, 50, $border, $fill);

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

以上示例的输出类似于:

示例输出:用颜色填充椭圆

注释

算法不会明确记住已经设置哪些像素,而是从像素的颜色判断,所以无法区分新设置的元素和已经存在的元素。这意味着选择任何图像中已经使用的填充颜色都可能会产生不期望的结果。

add a note

User Contributed Notes 2 notes

up
1
edrad at wanadoo dot fr
20 years ago
Very useful to build a pseudo-sphere with a color gradient...

<?php
$width
= 300;
$center = $width / 2;
$colordivs = 255 / $center;
$im = @imagecreate($width, $width);
$back_color = imagecolorallocate($im, 20, 30, 40);
imagefill($im, 0, 0, $back_color);
for (
$i = 0; $i <= $center; $i++)
{
$diametre = $width - 2 * $i;
$el_color = imagecolorallocate($im, $i * $colordivs, 0, 0);
imagearc($im, $center, $center, $diametre, $diametre, 0, 360, $el_color);
imagefilltoborder($im, $center, $center, $el_color, $el_color);
}
imagepng($im);
?>

Dark Skull Software
http://www.darkskull.net
up
0
admin at worldlanguages dot tk
19 years ago
In the example below, for those with newer GD versions, it makes more sense to replace:

imagearc($im, $center, $center, $diametre, $diametre, 0, 360, $el_color);

with:

imageellipse($im, $center, $center, $diametre, $diametre, $el_color);

This is obviously simpler.
To Top