如果我们要对图像进行处理,就如其它图像处理软件一样,需要创建一块画布。imagecreate() 和 imagecreatetruecolor() 函数用于创建一幅空白图像。
语法:
resource imagecreate( int x, int y )
参数 x ,y 分别为要创建图像的宽度和高度像素值,返回一个图像资源。
例子:
<?
header("Content-type: image/png");
//创建图像
$im = @imagecreate(200, 50) or die("创建图像资源失败");
//图片背景颜色
$bg = imagecolorallocate($im, 255, 255, 255);
//文字颜色
$text_color = imagecolorallocate($im, 0, 0, 255);
//水平画一行字,要输出中文等需要 TTF 字体支持的请使用 magettftext() 函数
imagestring($im, 5, 0, 0, "Hello world!", $text_color);
//以PNG格式输出图像
imagepng($im);
//销毁图像资源
imagedestroy($im);
?>
该例子以图像格式输出一行文字:Hello world! 。例子中用到的其他函数,将在后面逐一介绍。