函数:imagecreate()
适用版本:PHP 4, PHP 5, PHP 7
用法:imagecreate() 函数用于创建一个新的空白图像资源。它返回一个图像标识符,可以用于后续的图像处理操作。
语法:resource imagecreate(int $width, int $height)
参数:
- $width:图像的宽度(以像素为单位)
- $height:图像的高度(以像素为单位)
返回值:返回一个图像资源标识符,如果创建失败则返回 FALSE。
示例:
// 创建一个宽度为 200 像素,高度为 100 像素的空白图像
$image = imagecreate(200, 100);
// 设置图像的背景颜色为红色
$bgColor = imagecolorallocate($image, 255, 0, 0);
// 在图像上绘制一条蓝色的水平线
$lineColor = imagecolorallocate($image, 0, 0, 255);
imageline($image, 0, 50, 200, 50, $lineColor);
// 输出图像到浏览器
header('Content-Type: image/png');
imagepng($image);
// 释放图像资源
imagedestroy($image);
在上面的示例中,我们首先使用 imagecreate() 函数创建了一个宽度为 200 像素,高度为 100 像素的空白图像资源。然后,使用 imagecolorallocate() 函数为图像设置了一个红色的背景颜色。接下来,我们使用 imageline() 函数在图像上绘制了一条蓝色的水平线。最后,使用 imagepng() 函数将图像输出到浏览器,并使用 imagedestroy() 函数释放了图像资源。
注意:在使用 imagecreate() 函数时,需要确保 GD 库已经安装在 PHP 中。如果没有安装 GD 库,可以通过编辑 php.ini 文件并启用 GD 扩展来安装。