使用imagefilltoborder()(GD)函数在PHP中将图像填充为特定颜色。


**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
   // Load the GIF image from local drive folder.
   $img = imagecreatefromgif('C:\xampp\htdocs\Images\img39.gif');

   // Create the image colors
   $borderColor = imagecolorallocate($img, 0, 200, 0);
   $fillColor = imagecolorallocate($img, 122, 122, 122);

   // Add fill to border
   imagefilltoborder($img, 0, 0, $borderColor, $fillColor);

   // Show the output image
   header('Content-type: image/gif');
   imagepng($img);
?>

输出

在PHP中使用imagefilltoborder()函数之前的GIF图像。

在PHP中使用imagefilltoborder()函数之后的GIF图像。

示例 2:使用颜色填充椭圆

<?php
   // Created the image, set the background to gray color
   $img = imagecreatetruecolor(700, 500);
   imagefilledrectangle($img, 0, 0, 500, 500,imagecolorallocate($img, 122, 122, 122));

   // Draw an ellipse to fill with a black border.
   imageellipse($img, 300, 300, 300, 300, imagecolorallocate($img, 0, 0, 0));

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

   // Fill the selection
   imagefilltoborder($img, 300, 300, $border, $fill);

   // show the output image and free memory
   header('Content-type: image/gif');
   imagepng($img);
   imagedestroy($img);
?>

输出

更新于:2021年8月9日

293 次浏览

启动你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.