函数名:imagettftext()
适用版本:PHP 4 >= 4.0.7, PHP 5, PHP 7
用法:imagettftext() 函数用于将 TrueType 字体的文本添加到图像上。
语法:bool imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
参数:
返回值:成功时返回 true,失败时返回 false。
示例:
<?php
// 创建一个空白图像
$image = imagecreatetruecolor(400, 200);
// 设置背景颜色
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);
// 设置文本颜色
$textColor = imagecolorallocate($image, 0, 0, 0);
// 设置 TrueType 字体文件路径
$fontFile = 'path/to/font.ttf';
// 添加文本到图像
$text = 'Hello World!';
imagettftext($image, 20, 0, 50, 100, $textColor, $fontFile, $text);
// 输出图像
header('Content-Type: image/png');
imagepng($image);
// 释放图像资源
imagedestroy($image);
?>
以上示例创建了一个空白图像,设置了背景颜色和文本颜色,然后使用 imagettftext() 函数将文本 "Hello World!" 添加到图像上,并输出为 PNG 图片。请确保将 $fontFile 替换为真实的 TrueType 字体文件路径。