PHP Conference Japan 2024

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 定義。填滿的起始點為 xy(左上角為 0, 0),並以 color 顏色填滿該區域。

參數

影像

一個 GdImage 物件,由其中一個影像建立函式返回,例如 imagecreatetruecolor()

x

起始 x 座標。

y

起始 y 座標。

border_color

邊框顏色。使用 imagecolorallocate() 建立的顏色識別碼。

color

填滿顏色。使用 imagecolorallocate() 建立的顏色識別碼。

返回值

成功時返回 true,失敗時返回 false

更新日誌

版本 說明
8.0.0 image 現在需要一個 GdImage 實例;先前需要一個有效的 gd 資源

範例

範例 #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);
imagedestroy($im);
?>

以上範例將輸出類似以下的圖像

Output of example : Filling an ellipse with a color

注意事項

此演算法並不會明確記住哪些像素已經被設定,而是根據像素的顏色來推斷,因此它無法區分新設定的像素和已經存在的像素。這表示選擇任何已在圖像中使用的填充顏色可能會產生非預期的結果。

新增筆記

使用者貢獻的筆記 2 則筆記

edrad at wanadoo dot fr
21 年前
對於使用顏色漸層建立偽球體非常有用...

<?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 軟體
http://www.darkskull.net
admin at worldlanguages dot tk
20 年前
在下面的範例中,對於使用較新 GD 版本的使用者來說,將

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

替換為

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

顯然更簡單。
To Top