以下是一个使用PHP绘制简单图纸的实例,我们将使用GD库来创建和显示一个图像。在这个例子中,我们将绘制一个矩形和一个圆形。
1. 准备工作
确保你的PHP环境中安装了GD库。GD库是PHP中处理图像的标准库。

2. PHP代码
```php
// 创建一个空白图像
$width = 300;
$height = 300;
$image = imagecreatetruecolor($width, $height);
// 分配颜色
$background_color = imagecolorallocate($image, 255, 255, 255); // 白色
$rectangle_color = imagecolorallocate($image, 0, 0, 0); // 黑色
$circle_color = imagecolorallocate($image, 255, 0, 0); // 红色
// 填充背景颜色
imagefill($image, 0, 0, $background_color);
// 绘制矩形
imageline($image, 50, 50, 250, 50, $rectangle_color);
imageline($image, 250, 50, 250, 250, $rectangle_color);
imageline($image, 250, 250, 50, 250, $rectangle_color);
imageline($image, 50, 250, 50, 50, $rectangle_color);
// 绘制圆形
$center_x = 150;
$center_y = 150;
$radius = 100;
imagefilledellipse($image, $center_x, $center_y, $radius * 2, $radius * 2, $circle_color);
// 输出图像到浏览器
header('Content-Type: image/png');
imagepng($image);
// 释放内存
imagedestroy($image);
>
```
3. 表格展示
| 组件 | 位置 | 颜色 | 函数调用 |
|---|---|---|---|
| 背景 | 整个图像区域 | 白色 | imagefill($image,0,0,$background_color) |
| 矩形 | (50,50)至(250,250) | 黑色 | imageline() |
| 圆形 | 圆心(150,150),半径100 | 红色 | imagefilledellipse() |
以上PHP代码创建了一个300x300像素的图像,绘制了一个黑色矩形和一个红色圆形,并将其输出为PNG格式。这样,你就可以在浏览器中查看打印的图纸了。









