English | 简体中文 | 繁體中文
查询

imagecreatetruecolor()函数—用法及示例

「 创建一个真彩色图像资源 」


函数名:imagecreatetruecolor()

适用版本:PHP 4 >= 4.0.6, PHP 5, PHP 7

用法:imagecreatetruecolor() 函数用于创建一个真彩色图像资源。

语法:resource imagecreatetruecolor(int $width, int $height)

参数:

  • $width:图像的宽度,以像素为单位。
  • $height:图像的高度,以像素为单位。

返回值:

  • 成功时,返回一个图像资源标识符,表示创建的真彩色图像。
  • 失败时,返回 FALSE。

示例:

// 创建一个宽度为 400 像素,高度为 200 像素的真彩色图像
$image = imagecreatetruecolor(400, 200);

// 设置背景色为白色
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);

// 绘制一个红色的矩形
$red = imagecolorallocate($image, 255, 0, 0);
imagerectangle($image, 50, 50, 350, 150, $red);

// 在浏览器中输出图像
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

上述示例代码创建了一个宽度为 400 像素,高度为 200 像素的真彩色图像,并在图像中绘制了一个红色的矩形。最后,通过设置头部信息 Content-type: image/png,将图像以 PNG 格式输出到浏览器,并销毁图像资源。

补充纠错
上一个函数: imagecrop()函数
下一个函数: imagegetclip()函数
热门PHP函数
分享链接