CakeFest 2024: The Official CakePHP Conference

ImagickPixel::getColor

(PECL imagick 2, PECL imagick 3)

ImagickPixel::getColor色を返す

説明

public ImagickPixel::getColor(int $normalized = 0): array

ImagickPixel オブジェクトの色を、配列で返します。透明度が設定されている場合は、 4 つの要素からなる配列となります。

パラメータ

normalized

色を正規化する。 有効な値は 0, 1, 2 です。

normalized で有効な値の一覧
normalized 説明
0 RGB の値を 0 から 255 の間の値を取る int として返します (0 と 255 を含みます)。 アルファの値を 0 または 1int として返します。
1 RGBA の値を 0 から 1 の間の値を取る float として返します (0 と 1 を含みます)。
2 RGBA の値を 0 から 255 の間の値を取る int として返します (0 と 255 を含みます)。

戻り値

各チャネルの値を配列で返します。 エラー時には ImagickPixelException をスローします。

例1 基本的な Imagick::getColor() の使用例

<?php

// 定義済みの色 'brown' で ImagickPixel を作ります
$color = new ImagickPixel('brown');

// アルファ値を 25% にします
$color->setColorValue(Imagick::COLOR_ALPHA, 64 / 256.0);

$colorInfo = $color->getColor();

echo
"Standard values".PHP_EOL;
print_r($colorInfo);

$colorInfo = $color->getColor(1);

echo
"Normalized values:".PHP_EOL;
print_r($colorInfo);

?>

上の例の出力は以下となります。

Standard values
Array
(
    [r] => 165
    [g] => 42
    [b] => 42
    [a] => 0
)
Normalized values:
Array
(
    [r] => 0.64705882352941
    [g] => 0.16470588235294
    [b] => 0.16470588235294
    [a] => 0.25000381475547
)

add a note

User Contributed Notes 1 note

up
3
roman
10 years ago
In case you use default un-normalized getColor value the alpha value will always be either 0 or 1.

If you want to use real full-range 0-1 alpha channel on your 24bit transparent images use the alpha value from the normalized one, even if you use the rest of unnormalized data.

To copy a 24bit png with real alpha transparency, you would have to do this:
<?php

$im
=new Imagick( 'image.png' );
$iterator=$im->getPixelIterator();
foreach (
$iterator as $row=>$pixels) {
foreach (
$pixels as $column => $pixel ){
$un_color=$pixel->getColor(); //unnormalized color
$nor_color=$pixel->getColor(true); //normalized color
$pixel->setColor('rgba('.$un_color['r'].','.$un_color['g'].','.$un_color['b'].','.$nor_color['a'].')');
}
}
?>

If you use 'a' (alpha) value from the unnormalized color there will only be binary transparency.
To Top