
以下是一个使用PHP GD库绘制简单图形的实例,包括一个矩形和一个圆形。
实例1:绘制矩形
```php
// 创建一个画布
$width = 200;
$height = 200;
$image = imagecreatetruecolor($width, $height);
// 分配颜色
$red = imagecolorallocate($image, 255, 0, 0); // 红色
$white = imagecolorallocate($image, 255, 255, 255); // 白色
// 填充背景
imagefill($image, 0, 0, $white);
// 绘制矩形
imagerectangle($image, 50, 50, 150, 150, $red);
// 输出图像到浏览器
header('Content-Type: image/png');
imagepng($image);
// 释放内存
imagedestroy($image);
>
```
实例2:绘制圆形
```php
// 创建一个画布
$width = 200;
$height = 200;
$image = imagecreatetruecolor($width, $height);
// 分配颜色
$blue = imagecolorallocate($image, 0, 0, 255); // 蓝色
$white = imagecolorallocate($image, 255, 255, 255); // 白色
// 填充背景
imagefill($image, 0, 0, $white);
// 绘制圆形
imagefilledellipse($image, 100, 100, 100, 100, $blue);
// 输出图像到浏览器
header('Content-Type: image/png');
imagepng($image);
// 释放内存
imagedestroy($image);
>
```
表格总结
| 实例 | 图形 | 颜色 |
|---|---|---|
| 实例1 | 矩形 | 红色 |
| 实例2 | 圆形 | 蓝色 |









