PHP 8.3.4 Released!

Imagick::floodFillPaintImage

(PECL imagick 2 >= 2.3.0, PECL imagick 3)

Imagick::floodFillPaintImage対象にマッチする任意のピクセルの値を変更する

説明

public Imagick::floodFillPaintImage(
    mixed $fill,
    float $fuzz,
    mixed $target,
    int $x,
    int $y,
    bool $invert,
    int $channel = Imagick::CHANNEL_DEFAULT
): bool

対象にマッチするピクセルとその直接の近傍の任意のピクセルの色の値を変更します。 このメソッドは、廃止予定の Imagick::paintFloodFillImage() を置き換えるものです。 このメソッドは、ImageMagick バージョン 6.3.8 以降で Imagick をコンパイルした場合に使用可能です。

パラメータ

fill

塗りつぶし色を表す ImagickPixel オブジェクトあるいは文字列。

fuzz

あいまいさをあらわす量。たとえば、これを 10 に設定すると、 赤色の値が 100 の色と 102 の色は同じものとみなされます。

target

対象の色を表す ImagickPixel オブジェクトあるいは文字列。

x

開始位置の X 座標。

y

開始位置の Y 座標。

invert

true の場合は、対象の色にマッチしない任意のピクセルをペイントします。

channel

そのモードで有効なチャネル定数を指定します。 複数のチャネルを適用するには、チャネル定数 をビット演算子で組み合わせます。デフォルトは Imagick::CHANNEL_DEFAULT です。 チャネル定数 の一覧を参照ください。

戻り値

成功した場合に true を返します。

例1 Imagick::floodfillPaintImage() の例

<?php

/* 新しい imagick オブジェクトを作ります */
$im = new Imagick();

/* 赤、緑、青の画像を作ります */
$im->newImage(100, 50, "red");
$im->newImage(100, 50, "green");
$im->newImage(100, 50, "blue");

/* 画像をひとつにまとめます */
$im->resetIterator();
$combined = $im->appendImages(true);

/* この時点の画像を比較用に保存します */
$combined->writeImage("floodfillpaint_intermediate.png");

/* 塗りつぶす対象となるピクセル */
$x = 1;
$y = 1;

/* 塗りつぶす色を取得します */
$target = $combined->getImagePixelColor($x, $y);

/* 1,1 の一にあるピクセルと、対象の色にマッチするすべての
近傍ピクセルを塗りつぶします */
$combined->floodfillPaintImage("black", 1, $target, $x, $y, false);

/* 結果を保存します */
$combined->writeImage("floodfillpaint_result.png");
?>

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

Imagick::floodfillPaintImage() の出力例
Imagick::floodfillPaintImage() の出力例

add a note

User Contributed Notes 1 note

up
0
Anonymous
5 years ago
For fuzz, percentage or float values do not seem to work. The value is based on the intensity of the image colors.

The documentation states: "The amount of fuzz. For example, set fuzz to 10 and the color red at intensities of 100 and 102 respectively are now interpreted as the same color."

For those of us who are not graphics geeks, your color intensity might be something like 65535. In which case, to get just 10% fuzz, you need to set it to 6550.

You likely will not see any effect if you are using low numbers or floats, like 100, 20, or 0.8.

For example:
$im = new Imagick();
$transparentColor = new ImagickPixel('transparent');
$greenscreen = '#00FF08'; // Super bright green

$im->readImage("cartoon_dog.png"); // Cartoony dog with a black outline and a #00FF08 (super bright green) background.

// Replace the green background with transparent.

// Leaves significant green lines around the outline of the dog, which is unacceptable.
$im->floodFillPaintImage($transparentColor, 30, $greenscreen, 0, 0, false, Imagick::CHANNEL_ALPHA);

// Works as intended - removes all of the green background.
$im->floodFillPaintImage($transparentColor, 30000, $greenscreen, 0, 0, false, Imagick::CHANNEL_ALPHA);

Credit to the discussion here:
https://php5.kiev.ua/php7/imagick.painttransparentimage.html
To Top