使用 imagefilltoborder() (GD) 函数在 PHP 中将颜色填充为特定颜色。
phpserver side programmingprogramming
imagefilltoborder() 是 PHP 中的内置函数,用于使用特定颜色执行填充,其边框颜色由边框定义。填充的起点是 (x,y) 或左上角是 (0, 0),区域用颜色填充。
语法
bool imagefilltoborder(resource $image, int $x, int $y, int $border, int $color)
参数
imagefilltoborder() 有五个不同的参数:$image、$x、$y、$border 和 $color。
$image −它是图像资源。
$x − 指定开始的 x 坐标。
$y − 指定开始的 y 坐标。
$border − 指定边框颜色。
$color − 指定颜色。
返回值
成功时返回 True,失败时返回 False。
示例 1
<?php // 从本地驱动器文件夹加载 GIF 图像。 $img = imagecreatefromgif('C:\xampp\htdocs\Images\img39.gif'); // 创建图像颜色 $borderColor = imagecolorallocate($img, 0, 200, 0); $fillColor = imagecolorallocate($img, 122, 122, 122); // 为边框添加填充 imagefilltoborder($img, 0, 0, $borderColor, $fillColor); // 显示输出图像 header('Content-type: image/gif'); imagepng($img); ?>
输出
在 PHP 中使用 imagefilltoborder() 函数之前的 Gif 图像。
在 PHP 中使用 imagefilltoborder() 函数之后的 Gif 图像。
示例 2:用颜色填充椭圆
<?php // 创建图像,将背景设置为灰色 $img = imagecreatetruecolor(700, 500); imagefilledrectangle($img, 0, 0, 500, 500,imagecolorallocate($img, 122, 122, 122)); // 绘制一个椭圆,用黑色边框填充。 imageellipse($img, 300, 300, 300, 300, imagecolorallocate($img, 0, 0, 0)); // 设置边框并使用蓝色填充 $border = imagecolorallocate($img, 0, 0, 0); $fill = imagecolorallocate($img, 0, 0, 255); // 填充选择 imagefilltoborder($img, 300, 300, $border, $fill); // 显示输出图像和可用内存 header('Content-type: image/gif'); imagepng($img); imagedestroy($img); ?>