imagettftext
使用TrueType字体将文本写入图像
适用PHP版本:PHP 4及以上版本
imagettftext() 函数用于将文本绘制到图像上,支持TrueType字体(TTF)的渲染。
bool imagettftext(resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)
成功时返回true,失败时返回false。
以下示例将使用TrueType字体将文本绘制到图像上:
<?php // 创建一个空白图像 $image = imagecreatetruecolor(400, 200); <p>// 设置背景颜色<br> $bg_color = imagecolorallocate($image, 255, 255, 255);<br> imagefill($image, 0, 0, $bg_color);</p> <p>// 设置文本颜色(黑色)<br> $text_color = imagecolorallocate($image, 0, 0, 0);</p> <p>// 字体文件路径<br> $font_path = 'path/to/font.ttf';</p> <p>// 使用imagettftext绘制文本<br> imagettftext($image, 20, 0, 50, 100, $text_color, $font_path, 'Hello, World!');</p> <p>// 输出图像到浏览器<br> header('Content-Type: image/png');<br> imagepng($image);</p> <p>// 销毁图像资源<br> imagedestroy($image);<br> ?><br>