以下是一个使用PHP GD库绘制简单图形的实例。我们将使用GD库创建一个图像,并在图像上绘制一个矩形和圆形。
```php

// 创建一个新的图像
$width = 400;
$height = 300;
$image = imagecreatetruecolor($width, $height);
// 分配颜色
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$red = imagecolorallocate($image, 255, 0, 0);
// 填充背景颜色
imagefill($image, 0, 0, $white);
// 绘制矩形
imagerectangle($image, 50, 50, 350, 250, $black);
// 绘制圆形
$centerX = 200;
$centerY = 150;
$radius = 100;
imagefilledellipse($image, $centerX, $centerY, $radius * 2, $radius * 2, $red);
// 输出图像
header('Content-Type: image/png');
imagepng($image);
// 释放内存
imagedestroy($image);
>
```
下面是使用表格形式展示的代码结构:
| 函数/指令 | 描述 |
|---|---|
| `imagecreatetruecolor($width,$height)` | 创建一个新的图像 |
| `imagecolorallocate($image,$red,$green,$blue)` | 分配颜色 |
| `imagefill($image,$x,$y,$color)` | 填充图像区域 |
| `imagerectangle($image,$x1,$y1,$x2,$y2,$color)` | 绘制矩形 |
| `imagefilledellipse($image,$centerX,$centerY,$width,$height,$color)` | 绘制圆形 |
| `header('Content-Type:image/png')` | 设置HTTP响应头,告知浏览器内容类型为PNG |
| `imagepng($image)` | 输出图像 |
| `imagedestroy($image)` | 释放图像资源 |
以上代码创建了一个宽度为400像素、高度为300像素的图像,然后在图像上绘制了一个红色的圆形和一个黑色的矩形。输出这个图像并释放资源。



