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

imagestring()函数—用法及示例

「 在图像上绘制一行字符串 」


函数名称:imagestring()

适用版本:PHP 4, PHP 5, PHP 7

函数描述:imagestring() 函数用于在图像上绘制一行字符串。

语法:bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )

参数:

  • $image:图像资源,使用 imagecreatetruecolor() 或者 imagecreatefrom*() 函数创建。
  • $font:字体大小,可以为 1、2、3、4 或 5。
  • $x:字符串的起始 x 坐标。
  • $y:字符串的起始 y 坐标。
  • $string:要绘制的字符串。
  • $color:字符串的颜色,可以是通过 imagecolorallocate() 函数创建的颜色标识符。

返回值:成功时返回 true,失败时返回 false。

示例代码:

// 创建一个 200x100 的图像
$image = imagecreatetruecolor(200, 100);

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

// 设置字符串颜色为红色
$red = imagecolorallocate($image, 255, 0, 0);

// 在图像上绘制字符串
imagestring($image, 5, 50, 50, "Hello, PHP!", $red);

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

上述示例代码创建了一个 200x100 的图像,将背景色设置为白色,然后使用红色在图像上绘制了一行字符串 "Hello, PHP!",并将图像输出到浏览器。

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