操作步骤
- 创建画布
// imagecreatetruecolor($width,$height):创建画布,返回资源,返回图像标识符
$width = 500;
$height = 400;
$image = imagecreatetruecolor($width, $height);
- 创建颜色
$white = imagecolorallocate($image, 255, 255, 255);
$randColor = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
//imagefilledrectangle(): 绘制填充矩形
imagefilledrectangle($image, 0, 0, 500, 500, $white);
- 开始绘画
//imagechar():水平的绘制一个字符(只能绘制一个字符)
imagechar($image, 5, 50, 100, "L", $red);
//imagecharup():垂直的绘制一个字符(只能绘制一个字符)
imagecharup($image, 5, 100, 200, "Z", $blue);
//imagestring():水平绘制字符串
imagestring($image, 5, 200, 150, "imooc", $white);
//imagettftext(): 用 TrueType 字体向图像写入文本
imagettftext($image, 30, 0, 100, 200, $white, 'fonts/PingFang.TTF', 'This is LZQ show time');
- 通知浏览器显示图片
header('content-type:image/jpeg');
- 输出图片
//imagejpeg():输出图像
imagejpeg($image);
- 销毁资源
//imagedestory():销毁资源
imagedestory($image);