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
// 画像ハンドルを作成し、背景を白に設定します
$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);
?>

上の例の出力は、 たとえば以下のようになります。

出力例 : 楕円の塗りつぶし

注意

塗りつぶしのアルゴリズムは、 どのピクセルが既に設定されているかについて明示的に記憶しません。 むしろピクセルの色からそれを推測しようとします。 よって、そのピクセルが既に存在していたものか、 新たに設定されたものなのかは区別できません。 これは、画像で既に使われている色で画像を塗りつぶそうとすると、 望ましくない結果が得られることがあるということです。

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