imagefttext
使用FreeType 2使用字体将文本写入图像
PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8
imagefttext 函数用于在图像上绘制使用 FreeType 字体的文本。它允许在图像上添加自定义的文字内容,支持不同的字体、大小、颜色、角度等参数。
imagefttext( resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text [, array $extrainfo = array() ] )
返回图像资源,或者在失败时返回 FALSE。
以下是使用 imagefttext 函数的一个示例:
这段代码会创建一张空白的图像,设置字体大小、颜色、旋转角度等参数,并在指定位置绘制文字。
示例代码:
// 设置背景颜色为白色
$bg_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bg_color);
// 设置文本颜色为黑色
$text_color = imagecolorallocate($image, 0, 0, 0);
// 设置字体文件路径
$font = '/path/to/font.ttf';
// 使用 imagefttext 绘制文本
$text = "Hello, PHP!";
imagefttext($image, 20, 0, 10, 50, $text_color, $font, $text);
// 输出图像为 PNG 格式
header('Content-Type: image/png');
imagepng($image);
// 销毁图像资源
imagedestroy($image);
?>
说明: