Latest web development tutorials

PHP imagecolorclosestalpha – 取得與指定的顏色加透明度最接近的顏色的索引

PHP 圖像處理 PHP圖像處理

imagecolorclosestalpha — 取得與指定的顏色加透明度最接近的顏色的索引。

語法

int imagecolorclosestalpha ( resource $image , int $red , int $green , int $blue , int $alpha )

返回圖像調色板中與指定的RGB 值以及alpha 深度最"接近"的顏色。

參數

  • image由圖像創建函數(例如imagecreatetruecolor())返回的圖像資源。
  • red紅色成分的值。
  • green綠色成分的值。
  • blue藍色成分的值。
  • alpha一個介於0和127之間的值。0 表示完全不透明,127 表示完全透明。

顏色參數是介於0 和255 之間的整數,或者是介於0x00 和0xFF 之間的十六進制數。

返回值

返回調色板中最接近的顏色的索引。

實例

搜索圖像中的一組顏色。

<?php
// 从一个图像开始,并将其转换为一个基于调色板的图像
$im = imagecreatefrompng('figures/imagecolorclosest.png');
imagetruecolortopalette($im, false, 255);

// 搜索颜色 (RGB)
$colors = array(
    array(254, 145, 154, 50),
    array(153, 145, 188, 127),
    array(153, 90, 145, 0),
    array(255, 137, 92, 84)
);

// 循环遍历,查找调色板中最接近的颜色
// 返回搜索次数,搜索的 RGB 和最接近的匹配的 RGB
foreach($colors as $id => $rgb)
{
    $result = imagecolorclosestalpha($im, $rgb[0], $rgb[1], $rgb[2], $rgb[3]);
    $result = imagecolorsforindex($im, $result);
    $result = "({$result['red']}, {$result['green']}, {$result['blue']}, {$result['alpha']})";

    echo "#$id: 搜索 ($rgb[0], $rgb[1], $rgb[2], $rgb[3]); 最接近的匹配: $result。\n";
}

imagedestroy($im);
?>

以上實例的輸出類似於:

#0: 搜索 (254, 145, 154, 50); 最接近的匹配: (252, 150, 148, 0)。
#1: 搜索 (153, 145, 188, 127); 最接近的匹配: (148, 150, 196, 0)。
#2: 搜索 (153, 90, 145, 0); 最接近的匹配: (148, 90, 156, 0)。
#3: 搜索 (255, 137, 92, 84); 最接近的匹配: (252, 150, 92, 0)。

相關文章

PHP 圖像處理 PHP圖像處理