關於此函式的一些注意事項...
此函數僅適用於調色板顏色數量為 256 色或更少的圖像。您也不能在調色板顏色超過 256 色的真彩色 PNG 圖像上使用 imagetruecolortopalette() 來減少調色板,然後再呼叫此函數。如果您嘗試這樣做,即使圖像中存在這些顏色,imagecolorexact() 也會回報顏色不在圖像中!
1. 適用於 8 位元/256 色或更少的 png 檔案。
2. 適用於所有 gif 檔案。
3. 不適用於任何類型的 jpg/jpeg 圖像。
(PHP 4, PHP 5, PHP 7, PHP 8)
imagecolorexact — 取得指定顏色的索引
傳回指定顏色在影像調色板中的索引。
如果您是從檔案建立影像,則只會解析影像中使用的顏色。調色板中僅存在的顏色不會被解析。
傳回指定顏色在調色板中的索引,如果顏色不存在則傳回 -1。
範例 #1 從 GD 標誌取得顏色
<?php
// 設定影像
$im = imagecreatefrompng('./gdlogo.png');
$colors = Array();
$colors[] = imagecolorexact($im, 255, 0, 0);
$colors[] = imagecolorexact($im, 0, 0, 0);
$colors[] = imagecolorexact($im, 255, 255, 255);
$colors[] = imagecolorexact($im, 100, 255, 52);
print_r($colors);
// 從記憶體釋放
imagedestroy($im);
?>
上述範例將輸出類似以下的內容
Array ( [0] => 16711680 [1] => 0 [2] => 16777215 [3] => 6618932 )
關於此函式的一些注意事項...
此函數僅適用於調色板顏色數量為 256 色或更少的圖像。您也不能在調色板顏色超過 256 色的真彩色 PNG 圖像上使用 imagetruecolortopalette() 來減少調色板,然後再呼叫此函數。如果您嘗試這樣做,即使圖像中存在這些顏色,imagecolorexact() 也會回報顏色不在圖像中!
1. 適用於 8 位元/256 色或更少的 png 檔案。
2. 適用於所有 gif 檔案。
3. 不適用於任何類型的 jpg/jpeg 圖像。
一個根據 get 變數更改顏色的腳本
重要注意事項:我使用 png 並取得真正的紅色不太成功
gif 的效果好很多
<?php
//0 是黃色,1 是紅色,2 是藍色
$y = 1 - ceil($_GET["c"]/2);
$r = 1 - floor($_GET["c"]/2);
$b = floor($_GET["c"]/2);
$gd = imagecreatefromgif("example.gif");
imagecolorset($gd, imagecolorexact($gd, 255, 0, 0), $r*255, $y*255, $b*255);
imagecolorset($gd, imagecolorexact($gd, 191, 0, 0), $r*191, $y*191, $b*191);
imagecolorset($gd, imagecolorexact($gd, 128, 0, 0), $r*128, $y*128, $b*128);
imagecolorset($gd, imagecolorexact($gd, 255, 0, 0), $r*64, $y*64, $b*64);
header('Content-Type: image/gif');
imagegif($gd);
?>
<?php
$src = "../images/pic.gif";
$red = 9;
$green = 9;
$blue = 4;
$pic0026 = imagecreatefromgif( $src );
$ind = imagecolorexact( $pic, $red, $green, $blue );
echo '<img src="../images/pic.gif" border="0" alt="pic" title="檢視圖片" /><br /><br />';
echo "紅色 ( " . $red . " ) 綠色 ( " . $green . " ) 藍色 ( " . $blue . " )<br />-> 色盤索引 = " . $ind;
if ( $ind != -1 )
{
echo "<br />[ 顏色存在! ]";
}
else
{
echo "<br />[ 顏色不存在! ]";
}
imagedestroy( $pic );
?>