imagestring
水平绘制一个字符串
PHP 4.0.0 及以上版本
imagestring 函数在指定的图像上绘制一串文字。它使用内置的 5px 字体,在图像的指定位置绘制一行文本。
int imagestring(resource $image, int $font, int $x, int $y, string $text, int $color)
成功时返回文本的长度,失败时返回 -1。
下面的示例代码展示了如何使用 imagestring 函数在图像上绘制文本:
本示例创建了一幅 200x100 像素的图像,设置背景色为白色,然后在图像的坐标 (50, 40) 位置绘制了“Hello, World!”文本,字体大小为 5,文本颜色为黑色。
<?php
// 创建一个 200x100 的图像
$image = imagecreatetruecolor(200, 100);
// 分配背景色为白色
$background_color = imagecolorallocate($image, 255, 255, 255);
// 填充背景色
imagefill($image, 0, 0, $background_color);
// 分配文本颜色为黑色
$text_color = imagecolorallocate($image, 0, 0, 0);
// 在图像上绘制文本
imagestring($image, 5, 50, 40, "Hello, World!", $text_color);
// 设置 Content-Type 为图片
header("Content-Type: image/png");
// 输出图像
imagepng($image);
// 清理内存
imagedestroy($image);
?>
以上代码创建了一个 200x100 的图像,并在该图像的指定位置绘制了文本“Hello, World!”。