imagecolorallocate() 函数用于为图像分配颜色,返回一个标识符,代表了由给定的 RGB 成分组成的颜色,如果分配失败则返回 -1 。
语法:
int imagecolorallocate( resource image, int red, int green, int blue )
参数 red,green 和 blue 分别是所需要的颜色的 红,绿,蓝 成分,取值范围 0 - 255。
例子:
<?php
header("Content-type: image/png");
//创建图像
$im = @imagecreate(200, 50) or die("创建图像资源失败");
//图片背景颜色并填充
$bg = imagecolorallocate($im, 204, 204, 204);
//设定文字颜色
$red = imagecolorallocate($im, 255, 0, 0);
//水平画一行字
imagestring($im, 5, 0, 0, "Hello world!", $red);
//以PNG格式输出图像
imagepng($im);
//销毁图像资源
imagedestroy($im);
?>