PHP在没有图像资源的情况下分配颜色

2021-12-29 00:00:00 colors rgb php gd resources

你能在PHP GD中分配颜色吗a> 没有 图像资源?这应该是可能的,因为分配的颜色实际上是一个数字,对吗?

Can you allocate a color in PHP GD without an image resource? It should be possible because really an allocated color is a number, right?

$im = imagecreatetruecolor(100, 100);
$col = imagecolorallocate($im, 255, 0, 0);
print $col."<br/>";
$col2 = imagecolorallocate($im, 255, 0, 0);
print $col2."<br/>";
$im2 = imagecreatetruecolor(600, 100);
$col3 = imagecolorallocate($im, 255, 0, 0);
print $col3;

打印出来:

16711680

16711680

16711680

我想真正的问题是如何将 255、0 和 0 变成 16711680.

I guess what the real question is how 255, 0, and 0 are made into 16711680.

推荐答案

16711680(十进制)为 0x00FF0000(十六进制)

16711680 (decimal) is 0x00FF0000 (hexadecimal)

00 - Alpha 值 (0 dec)

00 - Alpha value (0 dec)

FF - 红色(255 dec)

FF - Red (255 dec)

00 - 绿色(0 dec)

00 - Green (0 dec)

00 - 蓝色 (0 dec)

00 - Blue (0 dec)

参见 http://www.php.net/manual/en/function.imagecolorallocatealpha.php 设置 alpha 字节

See http://www.php.net/manual/en/function.imagecolorallocatealpha.php to set the alpha byte

此外,要回答您的第一个问题 -- 是,您可以在没有图像资源的情况下创建颜色(因此无需调用 imagecolorallocate):

Also, to answer your first question -- yes, you can create a color without an image resource (and, consequently without a call to imagecolorallocate):

$col1 = 0x00FF0000;//红色

$col1 = 0x00FF0000; // Red

$col2 = 0x0000FF00;//绿色

$col2 = 0x0000FF00; // Green

//等等...

相关文章