PHP Conference Japan 2024

imagecolorallocate

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

imagecolorallocate為影像配置色彩

描述

imagecolorallocate(
    GdImage $image,
    int $red,
    int $green,
    int $blue
): int|false

返回一個顏色識別碼,代表由給定的 RGB 成分組成的顏色。

必須呼叫 imagecolorallocate() 以建立要在 image 代表的影像中使用的每個顏色。

注意:

第一次呼叫 imagecolorallocate() 會在基於調色盤的影像中填滿背景顏色,這些影像是使用 imagecreate() 建立的。

參數

image

一個 GdImage 物件,由影像建立函式之一傳回,例如 imagecreatetruecolor()

red

紅色成分的值。

green

綠色成分的值。

blue

藍色成分的值。

這些參數是介於 0 和 255 之間的整數,或是介於 0x00 和 0xFF 之間的十六進位數。

返回值

如果配置失敗,則返回顏色識別碼或 false

警告

此函式可能會返回布林值 false,但也可能返回評估為 false 的非布林值。請閱讀有關 布林值 的章節以取得更多資訊。使用 === 運算子來測試此函式的返回值。

變更日誌

版本 描述
8.0.0 image 現在需要一個 GdImage 實例;先前,需要一個有效的 gd 資源

範例

範例 1 imagecolorallocate() 範例

<?php

$im
= imagecreate(100, 100);

// 設定背景為紅色
$background = imagecolorallocate($im, 255, 0, 0);

// 設定一些顏色
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// 十六進位方式
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);

?>

參見

新增筆記

使用者貢獻筆記 21 筆記

22
jahservant 13 at gmail dot com
15 年前
請注意,您只能將 255 種顏色指派給任何影像調色盤。如果您嘗試指派更多顏色,imagecolorallocate() 將會失敗。

舉例來說,如果您是隨機配置顏色,最好檢查您是否已用盡所有可能的顏色。您可以使用 imagecolorclosest() 來取得最接近的已指派顏色。
<?php
//指派隨機 RGB 值
$c1 = mt_rand(50,200); //r(ed)
$c2 = mt_rand(50,200); //g(reen)
$c3 = mt_rand(50,200); //b(lue)
//測試我們是否已用完調色盤
if(imagecolorstotal($pic)>=255) {
//已用完調色盤;選擇最接近的已指派顏色
$color = imagecolorclosest($pic, $c1, $c2, $c3);
} else {
//尚未用完調色盤;指派新顏色
$color = imagecolorallocate($pic, $c1, $c2, $c3);
}
?>

此外,imagecolorallocate() 會在每次呼叫函式時指派新的顏色,即使該顏色已存在於調色盤中也是如此。
<?php
// [...]
imagecolorallocate($pic,125,125,125); //傳回 5
imagecolorallocate($pic,125,125,125); //傳回 6
imagecolorallocate($pic,125,125,125); //傳回 7
// [...]
imagecolorallocate($pic,125,125,125); //傳回 23
imagecolorallocate($pic,125,125,125); //傳回 25
// [...]
// 等等...
?>

因此,在這裡,imagecolorexact() 會很有用。
<?php
//檢查顏色是否已存在
$color = imagecolorexact($pic, $c1, $c2, $c3);
if(
$color==-1) {
//顏色不存在;配置新的顏色
$color = imagecolorallocate($pic, $c1, $c2, $c3);
}
?>

為了滿足技術宅的樂趣,我們可以將這兩個概念結合起來
<?php
//指定隨機 rgb 值
$c1 = mt_rand(50,200); //r(紅)
$c2 = mt_rand(50,200); //g(綠)
$c3 = mt_rand(50,200); //b(藍)
//從調色盤取得顏色
$color = imagecolorexact($pic, $c1, $c2, $c3);
if(
$color==-1) {
//顏色不存在...
//測試是否已用完調色盤
if(imagecolorstotal($pic)>=255) {
//調色盤已用完;挑選最接近的已配置顏色
$color = imagecolorclosest($pic, $c1, $c2, $c3);
} else {
//調色盤尚未用完;配置新顏色
$color = imagecolorallocate($pic, $c1, $c2, $c3);
}
}
?>

或者以函式的方式呈現
<?php
function createcolor($pic,$c1,$c2,$c3) {
//從調色盤取得顏色
$color = imagecolorexact($pic, $c1, $c2, $c3);
if(
$color==-1) {
//顏色不存在...
//測試是否已用完調色盤
if(imagecolorstotal($pic)>=255) {
//調色盤已用完;挑選最接近的已配置顏色
$color = imagecolorclosest($pic, $c1, $c2, $c3);
} else {
//調色盤尚未用完;配置新顏色
$color = imagecolorallocate($pic, $c1, $c2, $c3);
}
}
return
$color;
}

for(
$i=0; $i<1000; $i++) { //1000 因為它遠大於 255
//指定隨機 rgb 值
$c1 = mt_rand(50,200); //r(紅)
$c2 = mt_rand(50,200); //g(綠)
$c3 = mt_rand(50,200); //b(藍)
//產生顏色
$color = createcolor($pic,$c1,$c2,$c3);
//對顏色進行一些處理...
}
?>
9
Ludo
17 年前
當處理現有的 GIF 圖像時,如果不同顏色的數量已達到 GIF 格式的限制,imagecolorallocate 將不會使用您在參數中要求的顏色,它會套用黑色!

當從 GIF 圖像動態生成具有許多操作的圖像時,這會是一個問題。
要解決這個問題,您必須將 GIF 圖像轉換為 PNG 圖像,然後才能處理 PNG 圖像,一切都會順利。

例如:
<?php
// 首先,轉換為 PNG 圖像
$handle = imagecreatefromgif('my_image.gif');
imagepng($handle, 'my_image.png');
imagedestroy($handle);

// 然後,您可以處理它
$handle = imagecreatefrompng('my_image.png');
/*
* 處理圖像
*/
imagegif($handle);
?>
4
William Barath
17 年前
很多關於 hsv2rgb 的評論,但沒有可用的範例,所以這是我的:

<?php // 從 ImageMagick C 程式碼翻譯的 hsv2rgb 範例
function hsv2rgb($h, $s, $v)
{
$s /= 256.0;
if (
$s == 0.0) return array($v,$v,$v);
$h /= (256.0 / 6.0);
$i = floor($h);
$f = $h - $i;
$p = (integer)($v * (1.0 - $s));
$q = (integer)($v * (1.0 - $s * $f));
$t = (integer)($v * (1.0 - $s * (1.0 - $f)));
switch(
$i) {
case
0: return array($v,$t,$p);
case
1: return array($q,$v,$p);
case
2: return array($p,$v,$t);
case
3: return array($p,$q,$v);
case
4: return array($t,$p,$v);
default: return array(
$v,$p,$q);
}
}

$image = ImageCreateTrueColor(256,128);
for (
$y=0; $y<64; $y++) for($x=0; $x<256; $x++){
list(
$r,$g,$b) = hsv2rgb($x | 7,255,($y*4) |7);
$color = ($r << 16 ) | ($g << 8) | $b;
imagesetpixel($image, $x, $y-4, $color);
}
for (
$y=64; $y<128; $y++) for($x=0; $x<256; $x++){
list(
$r,$g,$b) = hsv2rgb($x|7,((127-$y)*4)|7,255);
$color = ($r << 16) | ($g << 8) | $b;
imagesetpixel($image, $x, $y-4, $color);
}
for (
$y=120; $y<128; $y++) for($x=0; $x<256; $x++){
$color = (($x |7) << 16) | (($x |7) << 8) | ($x |7);
imagesetpixel($image, $x, $y, $color);
}
header("Content-Type: image/png");
imagepng($image);
?>
3
jernberg 在 fairytale dot se
20 年前
這或許能幫助某些人,如何從 HTML 色彩定義中分配顏色

<?php
$fg
= "#ff0080";

$red = 100;
$green = 100;
$blue = 100;
if(
eregi( "[#]?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $fg, $ret ) )
{
$red = hexdec( $ret[1] );
$green = hexdec( $ret[2] );
$blue = hexdec( $ret[3] );
}

$text_color = ImageColorAllocate( $img1, $red, $green, $blue );
?>
1
andreoli dot carlo 在 libero dot it
19 年前
HSL 轉 RGB
(尚未最佳化,但功能正常)

<?php
function hslToRgb ($h, $s, $l) {
if (
$h>240 || $h<0) return array(0,0,0);
if (
$s>240 || $s<0) return array(0,0,0);
if (
$l>240 || $l<0) return array(0,0,0);
if (
$h<=40) {
$R=255;
$G=(int)($h/40*256);
$B=0;
}
elseif (
$h>40 && $h<=80) {
$R=(1-($h-40)/40)*256;
$G=255;
$B=0;
}
elseif (
$h>80 && $h<=120) {
$R=0;
$G=255;
$B=($h-80)/40*256;
}
elseif (
$h>120 && $h<=160) {
$R=0;
$G=(1-($h-120)/40)*256;
$B=255;
}
elseif (
$h>160 && $h<=200) {
$R=($h-160)/40*256;
$G=0;
$B=255;
}
elseif (
$h>200) {
$R=255;
$G=0;
$B=(1-($h-200)/40)*256;
}
$R=$R+(240-$s)/240*(128-$R);
$G=$G+(240-$s)/240*(128-$G);
$B=$B+(240-$s)/240*(128-$B);
if (
$l<120) {
$R=($R/120)*$l;
$G=($G/120)*$l;
$B=($B/120)*$l;
}
else {
$R=$l*((256-$R)/120)+2*$R-256;
$G=$l*((256-$G)/120)+2*$G-256;
$B=$l*((256-$B)/120)+2*$B-256;
}
if (
$R<0) $R=0;
if (
$R>255) $R=255;
if (
$G<0) $G=0;
if (
$G>255) $G=255;
if (
$B<0) $B=0;
if (
$B>255) $B=255;

return array((int)
$R,(int)$G,(int)$B);
}
?>
1
smoli at paranoya dot ch
20 年前
有些人可能想使用 HSV 色彩模型來繪製顏色選擇器和圓形

<?php
function &colormap_hsv_to_rgb($h, $s, $v)
{
$ret = new stdClass();

if(
$s == 0)
{
$ret->r = $v;
$ret->g = $v;
$ret->b = $v;

return
$ret;
}
else
{
$h = floatval($h) / 255.0;
$s = floatval($s) / 255.0;
$v = floatval($v) / 255.0;

$hue = $h;

if(
$hue == 1.0)
$hue = 0.0;

$hue *= 6.0;

$i = intval($hue);
$f = $hue - floatval($i);
$w = $v * (1.0 - $s);
$q = $v * (1.0 - ($s * $f));
$t = $v * (1.0 - ($s * (1.0 - $f)));

switch(
$i)
{
case
0: $ret->r = $v; $ret->g = $t; $ret->b = $w; break;
case
1: $ret->r = $q; $ret->g = $v; $ret->b = $w; break;
case
2: $ret->r = $w; $ret->g = $v; $ret->b = $t; break;
case
3: $ret->r = $w; $ret->g = $q; $ret->b = $v; break;
case
4: $ret->r = $t; $ret->g = $w; $ret->b = $v; break;
case
5: $ret->r = $v; $ret->g = $w; $ret->b = $q; break;
}
}

$ret->r = intval($ret->r * 255.0);
$ret->g = intval($ret->g * 255.0);
$ret->b = intval($ret->b * 255.0);

return
$ret;
}
?>
3
SW
18 年前
這個程式碼可以運作!它產生一個黑色圖片,並在圖片正中央以白色、反鋸齒的 Arial 字體顯示文字,且左右邊距相同,用於選單按鈕。

<?php

function createImgText ($string="", $fontsize=0, $marginX=0, $imgH=0 , $fontfile="", $imgColorHex="", $txtColorHex=""){
if(
$string!=""){
Header("Content-type: image/png");
//
$spacing = 0;
$line = array("linespacing" => $spacing);
$box = @imageftbbox($fontsize,0,$fontfile,$string,$line)
or die(
"ERROR");
$tw=$box[4]-$box[0]; //圖片寬度
$marginY = $imgH - (($imgH - $fontsize) / 2);
$imgWidth = $tw + (2*$marginX);
$im = ImageCreate($imgWidth, $imgH);
$int = hexdec($imgColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$black = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
$int = hexdec($txtColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$white = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
ImageFtText($im, $fontsize, 0, $marginX, $marginY, $white, $fontfile, $string, array());
ImagePng($im);
ImageDestroy($im);
}else{
echo
"ERROR!";
}
}
createImgText ("Hello World", 9, 10, 18, "arial.ttf", "000000", "FFFFFF");
?>
1
picklecj at rose-hulman dot edu
17 年前
另一個解決建立漸層時顏色限制問題的方案。此檔案會接收寬度 (px) 和左右顏色 (十六進位碼),並在僅分配 250 種顏色的情況下建立漸層。

<?php
$leftR
= hexdec(substr($_GET["left"],0,2));
$leftG = hexdec(substr($_GET["left"],2,2));
$leftB = hexdec(substr($_GET["left"],4,2));
$rightR = hexdec(substr($_GET["right"],0,2));
$rightG = hexdec(substr($_GET["right"],2,2));
$rightB = hexdec(substr($_GET["right"],4,2));
$image=imagecreate($_GET["width"],1);
for(
$i=0;$i<250;$i++) {
$colorset[$i] = imagecolorallocate($image, $leftR + ($i*(($rightR-$leftR)/250)), $leftG + ($i*(($rightG-$leftG)/250)), $leftB + ($i*(($rightB-$leftB)/250)));
}
for(
$i=0;$i<($_GET["width"]);$i++) {
imagesetpixel ($image, $i, 0, $colorset[(int)($i/($_GET["width"]/250))] );
}
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>

範例:gradient.php?width=640&left=000000&right=FF0000

產生一個寬度為 640 像素的圖片,從黑色漸變到紅色。
1
tyberis
20 年前
2 個函式,用於在 HSV 色彩空間(色相/飽和度/亮度)與 RGB(紅/綠/藍)色彩空間之間互相轉換。
<?php
// $c = array($red, $green, $blue)
// $red=[0..1], $green=[0..1], $blue=[0..1];
function rgb2hsv($c) {
list(
$r,$g,$b)=$c;
$v=max($r,$g,$b);
$t=min($r,$g,$b);
$s=($v==0)?0:($v-$t)/$v;
if (
$s==0)
$h=-1;
else {
$a=$v-$t;
$cr=($v-$r)/$a;
$cg=($v-$g)/$a;
$cb=($v-$b)/$a;
$h=($r==$v)?$cb-$cg:(($g==$v)?2+$cr-$cb:(($b==$v)?$h=4+$cg-$cr:0));
$h=60*$h;
$h=($h<0)?$h+360:$h;
}
return array(
$h,$s,$v);
}

// $c = array($hue, $saturation, $brightness)
// $hue=[0..360], $saturation=[0..1], $brightness=[0..1]
function hsv2rgb($c) {
list(
$h,$s,$v)=$c;
if (
$s==0)
return array(
$v,$v,$v);
else {
$h=($h%=360)/60;
$i=floor($h);
$f=$h-$i;
$q[0]=$q[1]=$v*(1-$s);
$q[2]=$v*(1-$s*(1-$f));
$q[3]=$q[4]=$v;
$q[5]=$v*(1-$s*$f);
//return(array($q[($i+4)%5],$q[($i+2)%5],$q[$i%5]));
return(array($q[($i+4)%6],$q[($i+2)%6],$q[$i%6])); //[1]
}
}
?>

[1] - 編輯者註記:這是來自 "hc at hob(removethis)soft dot net" 的修正。
1
chris at drunkenpirates dot co dot uk
21 年前
<?php

/*
一個結合使用 ImageColorAllocate、Imagesetpixel、Imagecopyresized 和一些基本三角函數的範例
*/

Header("Content-type: image/png");

$height = 128;
$width = 128;

$imA = ImageCreate($width, $height);
$imB = ImageCreate($width*4, $height*4);
$bckA = ImageColorAllocate($imA, 0,0,0);
$bckB = ImageColorAllocate($imB, 0,0,0);

//產生灰階調色盤

for($c=0;$c<256;$c++){
ImageColorAllocate($imA, $c, $c, $c);
}

//產生資料

$m=rand(0,10);
for(
$c=0;$c<128;$c++){
$s= (sin( deg2rad($c*360*$m/128) )+1)*127;
$col_arr[$c]=$s;
}
for(
$y=0;$y<$height;$y++){
for(
$x=0;$x<$width;$x++){
$imgA[$x][$y]=$col_arr[$x];
}
}
for(
$y=0;$y<$height;$y++){
for(
$x=0;$x<$width;$x++){
$imgB[$x][$y]=$col_arr[$y];
}
}

//設定像素

for($y=0;$y<$height;$y++){
for(
$x=0;$x<$width;$x++){
$imgC[$x][$y]=$imgA[$x][$y]+$imgB[$x][$y];
$s=$imgC[$x][$y]/2;
Imagesetpixel($imA,$x,$y,$s);
}
}

//調整圖片大小以顯示

Imagecopyresized ($imB, $imA, 0, 0, 0, 0, $width*4, $height*4, $width, $width);
ImagePNG($imB);
?>
1
bisqwit at iki dot fi
22 年前
實際上,你不能為調色盤圖片 (ImageCreate) 分配超過 256 種顏色。
請改用 ImageCreateTrueColor。要使其運作,你需要 PHP 中有 libgd 版本 2 的支援。
1
behun at webconsult dot sk
18 年前
此外,當您需要超過 256 種顏色時,請使用 imagecreatetruecolor 函數。使用此函數您可以運用無限數量的顏色。
1
aaron at parecki dot com
19 年前
這將讓您將圖片著色為任何特定顏色。原始圖片的黑色會變成您指定的顏色,而白色則保持白色。這對於為灰階圖片著色效果最佳。

<?php

$r
= 224;
$g = 192;
$b = 0;
$source_file = "picture.jpg";

$im_src = ImageCreateFromJpeg($source_file);
$im_tint = ImageCreate(imagesx($im_src),imagesy($im_src));
for (
$c = 0; $c < 255; $c++) {
ImageColorAllocate($im_tint, max($r,$c), max($g,$c), max($b,$c));
}
ImageCopyMerge($im_tint,$im_src,0,0,0,0, imagesx($im_src), imagesy($im_src), 100);
ImageDestroy($im_src);

header("Content-type: image/jpeg");
imagejpeg($im_tint);

?>
1
phillip dot gooch at gmail dot com
13 年前
如果您遇到無法分配您想要的顏色的情況,可能是因為您的圖片顏色分配表。GIF 和 8 位元 PNG 圖片非常容易受到此影響。

如果您使用的是 GIF 和 PNG,請嘗試從表格中刪除一種顏色,應該可以讓您分配另一種顏色。
2
mlse
19 年前
另一個更通用的主題變化,使用與內建函數 hexdec 和 dechex 相同的命名慣例...

原型

array hexrgb ( string hex_string )

回傳值

一個關聯陣列,其中包含 hex_string 中指定的 RGB 元件。

hexrgb() 範例

<?php
$rgb
= hexrgb("0xAABBCC");
print_r($rgb);
?>

輸出是

陣列
(
[red] => 170
[green] => 187
[blue] => 204
)

實作

<?php
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);

return array(
"red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
?>

然後,hexdec 的輸出可以傳遞給 imagecolorallocate 並根據需要進行操作。
2
mail at kailashnadh dot name
19 年前
這個漂亮的功能會產生給定圖片的負片!

<?php

/********************************

Code by Kailash Nadh
http://kailashnadh.name

usage:
img2neg("my_pic.jpg");

*********************************/

function img2neg($pic) {

header("Content-type: image/jpeg");

$source=imagecreatefromjpeg($pic); // 來源
$width=imagesx($source); $height=imagesy($source);

$im = imagecreatetruecolor($width, $height); // 我們正在製作的負片影像

for($y=0; $y < $height; $y++) {
for(
$x=0; $x < $width; $x++) {

$colors=imagecolorsforindex($source, imagecolorat($source, $x,$y));

// 這會讓顏色變成負片
$r=255-$colors['red'];
$g=255-$colors['green'];
$b=255-$colors['blue'];
$test=imagecolorallocate($im, $r,$g,$b);
imagesetpixel($im,$x, $y, $test);
}
}

imagejpeg($im);
imagedestroy($im);
}

?>
2
mv at brazil dot com
19 年前
<?php

/**
* 使用 lib GD 建立圖片長條圖
* 例如。 <img src="color_sample.php?color=FF0000" width="10 height="30">
*/

// 分割 HTML 顏色表示法
$hexcolor = str_split($_GET["color"], 2);

// 將 HEX 值轉換為 DECIMAL
$bincolor[0] = hexdec("0x{$hexcolor[0]}");
$bincolor[1] = hexdec("0x{$hexcolor[1]}");
$bincolor[2] = hexdec("0x{$hexcolor[2]}");

$im = ImageCreate(100, 100);
$colorallocate = ImageColorAllocate($im, $bincolor[0], $bincolor[1], $bincolor[2]);
ImageFilledRectangle($im, 0, 0, 100, 100, $colorallocate);
header('Content-Type: image/png');
ImagePNG($im);

?>
0
Zigbigidorlu at hotmail dot com
19 年前
這是一個非常簡單且非常有效的程式碼,可將 HEX 顏色變更為 RGB。

<?php
function HEX2RGB($color){
$color_array = array();
$hex_color = strtoupper($color);
for(
$i = 0; $i < 6; $i++){
$hex = substr($hex_color,$i,1);
switch(
$hex){
case
"A": $num = 10; break;
case
"B": $num = 11; break;
case
"C": $num = 12; break;
case
"D": $num = 13; break;
case
"E": $num = 14; break;
case
"F": $num = 15; break;
default:
$num = $hex; break;
}
array_push($color_array,$num);
}
$R = (($color_array[0] * 16) + $color_array[1]);
$G = (($color_array[2] * 16) + $color_array[3]);
$B = (($color_array[4] * 16) + $color_array[5]);
return array(
$R,$G,$B);
unset(
$color_array,$hex,$R,$G,$B);
}
?>
0
匿名
19 年前
當您使用真彩色影像時,您也可以使用位元運算來產生顏色
<?php
$color
= ($r << 16) | ($g << 8) | $b; // 2261213
?>
這與 truecolor 影像中的 imagecolorallocate() 函數相同!
-1
leif at harmsen dot net
21 年前
我無法讓任何已發佈的將顏色轉換為灰階的方法運作。問題似乎是 gd 在不同版本之間從 jpeg 建立影像的方式不一致。最終我寫了自己的方法,對我來說是有效的 - 此方法會先配置 256 色調色盤。您也可以在使用 imagecolorallocate 之前使用單獨的 $r、$g、$b 變數來調整影像的色調或色彩。

<?php
$resource
= 'whatever.jpg';
$im_size = GetImageSize($resource);
$imageWidth = $im_size[0];
$imageHeight = $im_size[1];
$im = imageCreate($imageWidth,$imageHeight);
for (
$c = 0; $c < 256; $c++) {
ImageColorAllocate($im, $c,$c,$c);
}
$im2 = ImageCreateFromJpeg($resource);
ImageCopyMerge($im,$im2,0,0,0,0, $imageWidth, $imageHeight, 100);
ImageDestroy($im2);
?>

繼續使用 $im 作為您的影像,它現在是灰階了....
-3
divinity76 at gmail dot com
8 年前
您的顏色索引空間非常有限(在我的系統上,有超過 10 GB 的可用記憶體、cli,沒有記憶體限制,有 255 個索引),當沒有更多索引可用時,imagecolorallocate 將會傳回 false。當您使用相同的 r/g/b 建立 2 個索引時,您會浪費這個非常有限的空間。下面的函數應該永遠不會失敗,而且永遠不會浪費任何顏色索引空間。如果已經有一個具有 rgb 的索引,它將傳回現有的索引,否則它將嘗試配置 1 個,如果配置失敗(大概是因為所有索引空間都已用完),它將傳回最接近您要求的匹配,並透過 $couldFindExact 警告您。

function myimagecolorallocate($gd,int $red,int $green,int $blue,bool &$couldFindExact=null):int{
$ret=imagecolorexact($gd, $red, $green, $blue);
if($ret===-1){
$ret=imagecolorallocate($gd, $red, $green, $blue);
if($ret===false){
$couldFindExact=false;//顏色索引不足(預設為 255 個索引..希望我知道為什麼)
$ret=imagecolorclosest($gd, $red, $green, $blue);
} else {
$couldFindExact=true;
}
} else {
$couldFindExact=true;
}
return $ret;
}
To Top