以下是一个简单的PHP实例,演示如何使用实例变量$im进行图像处理。
```php
// 创建一个新的GD图像资源
$im = imagecreatetruecolor(100, 100);
// 分配颜色
$background_color = imagecolorallocate($im, 255, 255, 255);
$red_color = imagecolorallocate($im, 255, 0, 0);
// 填充背景色
imagefill($im, 0, 0, $background_color);
// 在图像上画一个红色的矩形
imagefilledrectangle($im, 0, 0, 99, 99, $red_color);
// 输出图像到浏览器
header('Content-Type: image/png');
imagepng($im);
// 释放图像资源
imagedestroy($im);
>
```
| 变量 | 类型 | 描述 |
|---|---|---|
| $im | resource | GD图像资源,用于存储图像信息 |
| $background_color | int | 背景颜色的分配值 |
| $red_color | int | 红色的分配值 |
| imagecreatetruecolor() | resource | 创建一个新的GD图像资源 |
| imagecolorallocate() | int | 分配颜色 |
| imagefill() | void | 用指定的颜色填充图像区域 |
| imagefilledrectangle() | void | 在图像上画一个填充的矩形 |
| header() | void | 发送原始的HTTP头部信息 |
| imagepng() | void | 输出图像到浏览器 |
| imagedestroy() | void | 释放图像资源 |
这个例子中,我们首先创建了一个新的GD图像资源$im,然后分配了背景色和红色。接着,我们使用imagefill()和imagefilledrectangle()函数填充背景色,并在图像上画了一个红色的矩形。我们使用header()函数设置内容类型,然后使用imagepng()输出图像到浏览器,并在最后使用imagedestroy()释放图像资源。