2024 年 PHP Conference Japan

imagecolorsforindex

(PHP 4, PHP 5, PHP 7, PHP 8)

imagecolorsforindex取得索引的顏色

說明

imagecolorsforindex(GdImage $image, int $color): array

取得指定索引的顏色。

參數

image

一個 GdImage 物件,由其中一個影像建立函式返回,例如 imagecreatetruecolor()

color

顏色索引。

返回值

返回一個關聯式陣列,其中包含 red(紅色)、green(綠色)、blue(藍色)和 alpha(透明度)鍵,這些鍵包含指定顏色索引的相應值。

更新日誌

版本 說明
8.0.0 image 現在需要一個 GdImage 實例;先前需要一個有效的 gd 資源
8.0.0 如果 color 超出範圍,imagecolorsforindex() 現在會拋出 ValueError 異常;先前會返回 false

範例

範例 #1 imagecolorsforindex() 範例

<?php

// 開啟影像
$im = imagecreatefrompng('nexen.png');

// 取得顏色
$start_x = 40;
$start_y = 50;
$color_index = imagecolorat($im, $start_x, $start_y);

// 將其轉換為人類可讀的格式
$color_tran = imagecolorsforindex($im, $color_index);

// 它是什麼?
print_r($color_tran);

?>

上述範例將輸出類似以下的內容:

Array
(
   [red] => 226
   [green] => 222
   [blue] => 252
   [alpha] => 0
)

另請參閱

新增註釋

使用者貢獻的註釋 10 則註釋

matrebatre
16 年前
請注意

<?php
$rgba
= imagecolorat($image, $x, $y);
$r = ($rgba >> 16) & 0xFF;
$g = ($rgba >> 8) & 0xFF;
$b = $rgba & 0xFF;
$a = ($rgba & 0x7F000000) >> 24;
?>

這只適用於真彩色圖像。例如 GIF 圖像,這將會產生奇怪的結果。對於 GIF 圖像,您應該始終使用 imagecolorsforindex()。
slepichev at yahoo dot com
17 年前
如果您想更改特定顏色的強度或亮度級別,您需要將顏色格式從 RGB 轉換為 HSL。
以下函數將 RGB 陣列(紅、綠、藍)轉換為 HSL 陣列(色相、飽和度、亮度)
<?php
/**
* Convert RGB colors array into HSL array
*
* @param array $ RGB colors set
* @return array HSL set
*/
function rgb2hsl($rgb){

$clrR = ($rgb[0] / 255);
$clrG = ($rgb[1] / 255);
$clrB = ($rgb[2] / 255);

$clrMin = min($clrR, $clrG, $clrB);
$clrMax = max($clrR, $clrG, $clrB);
$deltaMax = $clrMax - $clrMin;

$L = ($clrMax + $clrMin) / 2;

if (
0 == $deltaMax){
$H = 0;
$S = 0;
}
else{
if (
0.5 > $L){
$S = $deltaMax / ($clrMax + $clrMin);
}
else{
$S = $deltaMax / (2 - $clrMax - $clrMin);
}
$deltaR = ((($clrMax - $clrR) / 6) + ($deltaMax / 2)) / $deltaMax;
$deltaG = ((($clrMax - $clrG) / 6) + ($deltaMax / 2)) / $deltaMax;
$deltaB = ((($clrMax - $clrB) / 6) + ($deltaMax / 2)) / $deltaMax;
if (
$clrR == $clrMax){
$H = $deltaB - $deltaG;
}
else if (
$clrG == $clrMax){
$H = (1 / 3) + $deltaR - $deltaB;
}
else if (
$clrB == $clrMax){
$H = (2 / 3) + $deltaG - $deltaR;
}
if (
0 > $H) $H += 1;
if (
1 < $H) $H -= 1;
}
return array(
$H, $S, $L);
}
?>
joe dot scylla at gmail dot com
17 年前
雖然使用以下程式碼取得像素的 Alpha 透明度相當簡單直觀:

<?php
$rgba
= imagecolorsforindex($image, imagecolorat($image, $x, $y));
$alpha = $rgba["alpha"];
?>

但您應該使用 imagecolorat 命令的返回值來取得 Alpha 透明度,如下列程式碼所示,因為它速度更快,並且如果您處理圖像的每個像素,將會產生重大影響

<?php
$rgba
= imagecolorat($image, $x, $y);
$alpha = ($rgba & 0x7F000000) >> 24;
?>
hofstadler dot andi at gmx dot at
16 年前
我稍微優化了 slepichev 的 rgb2hsl 函數,使其更簡潔,也希望能更快一些

<?php
/**
* Convert RGB colors array into HSL array
*
* @param array $ RGB colors set, each color component with range 0 to 255
* @return array HSL set, each color component with range 0 to 1
*/
function rgb2hsl($rgb){
$clrR = ($rgb[0]);
$clrG = ($rgb[1]);
$clrB = ($rgb[2]);

$clrMin = min($clrR, $clrG, $clrB);
$clrMax = max($clrR, $clrG, $clrB);
$deltaMax = $clrMax - $clrMin;

$L = ($clrMax + $clrMin) / 510;

if (
0 == $deltaMax){
$H = 0;
$S = 0;
}
else{
if (
0.5 > $L){
$S = $deltaMax / ($clrMax + $clrMin);
}
else{
$S = $deltaMax / (510 - $clrMax - $clrMin);
}

if (
$clrMax == $clrR) {
$H = ($clrG - $clrB) / (6.0 * $deltaMax);
}
else if (
$clrMax == $clrG) {
$H = 1/3 + ($clrB - $clrR) / (6.0 * $deltaMax);
}
else {
$H = 2 / 3 + ($clrR - $clrG) / (6.0 * $deltaMax);
}

if (
0 > $H) $H += 1;
if (
1 < $H) $H -= 1;
}
return array(
$H, $S,$L);
}
?>
derek at idreams dot co dot uk
17 年前
早期的微軟深褐色範例似乎有一個使其偏粉紅色的因素... 這裡是一個修改後的範例,它只使用微軟深褐色(根據維基百科深褐色條目)

<?
function imagetosepia(&$img) {
if (!($t = imagecolorstotal($img))) {
$t = 256;
imagetruecolortopalette($img, true, $t);
}
$total = imagecolorstotal( $img );
for ( $i = 0; $i < $total; $i++ ) {
$index = imagecolorsforindex( $img, $i );
$red = ( $index["red"] * 0.393 + $index["green"] * 0.769 + $index["blue"] * 0.189 );
$green = ( $index["red"] * 0.349 + $index["green"] * 0.686 + $index["blue"] * 0.168 );
$blue = ( $index["red"] * 0.272 + $index["green"] * 0.534 + $index["blue"] * 0.131 );
if ($red > 255) { $red = 255; }
如果 ($green > 255) { $green = 255; }
如果 ($blue > 255) { $blue = 255; }
imagecolorset( $img, $i, $red, $green, $blue );
}
}
?>
admin at phpgfx dot com
17 年前
這是使用 Microsoft 定義的深褐色濾鏡

<?php

函式 imagesepia( $img ) {
$total = imagecolorstotal( $img );
for (
$i = 0; $i < $total; $i++ ) {
$index = imagecolorsforindex( $img, $i );
$red = ( $index["red"] * 0.393 + $index["green"] * 0.769 + $index["blue"] * 0.189 ) / 1.351;
$green = ( $index["red"] * 0.349 + $index["green"] * 0.686 + $index["blue"] * 0.168 ) / 1.203;
$blue = ( $index["red"] * 0.272 + $index["green"] * 0.534 + $index["blue"] * 0.131 ) / 2.140;
imagecolorset( $img, $i, $red, $green, $blue );
}
}

?>
adspeed.com
19 年前
修正 m4551 at abasoft dot it 的範例

ImageTrueColorToPalette($im,1,$t);

可能會產生比 $t 少的顏色,所以 for 迴圈應該呼叫 "$i < ImageColorsTotal($im)" 而不是 "$i < $t" 以確保不會出現警告:顏色索引 [0-9] 超出範圍
strozek(a)deas()harvard()edu
20 年前
關於 m4551 的轉換方法 -- CCIR 批准的 RGB 轉灰階轉換方法如下:

灰階成分 = 0.2125*R + 0.7154*G + 0.0721*B

(參見適用於現代螢幕的 CCIR 建議 709)
m4551 at abasoft dot it
20 年前
這是一個將圖像灰階化的函式,即使來源是真彩色 (jpeg 或 png) 也適用。

品質略差,但速度很快...

函式 imagegreyscale(&$img, $dither=1) {
if (!($t = imagecolorstotal($img))) {
$t = 256;
imagetruecolortopalette($img, $dither, $t);
}
for ($c = 0; $c < $t; $c++) {
$col = imagecolorsforindex($img, $c);
$min = min($col['red'],$col['green'],$col['blue']);
$max = max($col['red'],$col['green'],$col['blue']);
$i = ($max+$min)/2;
imagecolorset($img, $c, $i, $i, $i);
}
}
tim at leethost dot com
17 年前
這裡提供一個更好的灰階、深褐色以及泛用色調調整函式。這個函式更好,因為:

1) 可用於真彩色影像(其他深褐色程式碼無法做到)。
2) 提供更優良的灰階轉換(是的,我說「更優良」)。其他的灰階程式碼使用 `imagetruecolortopalette`,它在灰階轉換上效果不佳。
3) 其他深褐色程式碼的顏色太過鮮豔,對我來說有點過頭。這個函式讓您可以選擇性地將灰階色調設定為任何您想要的顏色。
4) 單一函式即可實現灰階、深褐色和任何您能想到的色調調整。

以下是一些範例:

`imagegrayscaletint ($img);` // 灰階,無色調調整
`imagegrayscaletint ($img, 304, 242, 209);` // 我用於深褐色的設定
`imagegrayscaletint ($img, 0, 0, 255);` // 藍莓色的影像

用於色調調整的 RGB 值通常介於 0 到 255 之間。但是,您可以使用大於 255 的值來提亮和「加深」影像。上面的深褐色範例略有展現,以下範例更清楚地示範了提亮影像並稍微加深亮區的效果。

`imagegrayscaletint ($img, 400, 400, 400);` // 提亮影像
`imagegrayscaletint ($img, 127, 127, 127);` // 加深影像

<?
`function imagegrayscaletint (&$img, $tint_r = 255, $tint_g = 255, $tint_b = 255) {`
`$width = imagesx($img); $height = imagesy($img);`
`$dest = imagecreate ($width, $height);`
`for ($i=0; $i<256; $i++) imagecolorallocate ($dest, $i, $i, $i);`
`imagecopyresized ($dest, $img, 0, 0, 0, 0, $width, $height, $width, $height);`
`for ($i = 0; $i < 256; $i++) imagecolorset ($dest, $i, min($i * abs($tint_r) / 255, 255), min($i * abs($tint_g) / 255, 255), min($i * abs($tint_b) / 255, 255));`
`$img = imagecreate ($width, $height);`
`imagecopy ($img, $dest, 0, 0, 0, 0, $width, $height);`
`imagedestroy ($dest);`
}
?>
To Top