imagefilltoborder
(PHP 4, PHP 5, PHP 7, PHP 8)
imagefilltoborder — 漫水填充特定颜色
说明
imagefilltoborder() 执行漫水填充,其边框颜色由 border_color
定义。填充的起点是 x
, y
(左上角是0, 0),区域用颜色
color
填充。【注:边界内的所有颜色都会被填充。如果指定的边界色和该点颜色相同,则没有填充。如果图像中没有该边界色,则整幅图像都会被填充。】
参数
-
image
由图象创建函数(例如imagecreatetruecolor())返回的 GdImage 对象。
x
-
起点的 x 坐标。
y
-
起点的 y 坐标。
border_color
-
边框颜色。颜色标识符使用 imagecolorallocate() 创建。
color
-
填充颜色。颜色标识符使用 imagecolorallocate() 创建。
示例
示例 #1 用颜色填充椭圆
<?php
// 创建图像句柄,并将背景设置为白色
$im = imagecreatetruecolor(100, 100);
imagefilledrectangle($im, 0, 0, 100, 100, imagecolorallocate($im, 255, 255, 255));
// 绘制椭圆,并用黑色边框填充
imageellipse($im, 50, 50, 50, 50, imagecolorallocate($im, 0, 0, 0));
// 设置边框和填充颜色
$border = imagecolorallocate($im, 0, 0, 0);
$fill = imagecolorallocate($im, 255, 0, 0);
// 填充选区
imagefilltoborder($im, 50, 50, $border, $fill);
// 输出
header('Content-type: image/png');
imagepng($im);
?>
以上示例的输出类似于:

注释
算法不会明确记住已经设置哪些像素,而是从像素的颜色判断,所以无法区分新设置的元素和已经存在的元素。这意味着选择任何图像中已经使用的填充颜色都可能会产生不期望的结果。
+添加备注
用户贡献的备注 2 notes
edrad at wanadoo dot fr ¶
21 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
admin at worldlanguages dot tk ¶
20 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.
备份地址:http://www.lvesu.com/blog/php/function.imagefilltoborder.php