函数名:imagefttext()
适用版本:PHP 4 >= 4.0.7, PHP 5, PHP 7
用法:imagefttext() 函数在图像上使用 TrueType 字体绘制文本。它返回一个布尔值,表示文本是否成功绘制到图像上。
语法:bool imagefttext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text [, array $extrainfo] )
参数:
- $image:图像资源,使用 imagecreatetruecolor() 创建。
- $size:字体大小,以像素为单位。
- $angle:文本倾斜角度,以度为单位。正值为逆时针旋转。
- $x:文本起始位置的 x 坐标。
- $y:文本起始位置的 y 坐标。
- $color:文本颜色,使用 imagecolorallocate() 创建。
- $fontfile:TrueType 字体文件的路径。
- $text:要绘制的文本。
- $extrainfo(可选):一个关联数组,用于指定额外的绘制参数,如字符间距、行高等。
返回值:如果成功绘制文本,则返回 true,否则返回 false。
示例:
// 创建一个新的图像资源
$image = imagecreatetruecolor(400, 200);
// 分配文本颜色
$color = imagecolorallocate($image, 255, 255, 255);
// 指定字体文件路径
$fontfile = 'arial.ttf';
// 绘制文本到图像上
imagefttext($image, 20, 0, 50, 100, $color, $fontfile, 'Hello World');
// 输出图像到浏览器
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
以上示例创建一个大小为 400x200 像素的图像,使用 Arial 字体绘制了一个大小为 20 像素的 "Hello World" 文本,起始位置为 (50, 100) 坐标。最后将图像以 PNG 格式输出到浏览器,并销毁图像资源。