imagefttext

(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)

imagefttext使用 FreeType 2 字体将文本写入图像

说明

imagefttext(
    GdImage $image,
    float $size,
    float $angle,
    int $x,
    int $y,
    int $color,
    string $font_filename,
    string $text,
    array $options = []
): array|false

注意:

在 PHP 8.0.0 之前,imagefttext()imagettftext() 的扩展变体,它还支持 options。 自 PHP 8.0.0 起,imagettftext()imagefttext() 的别名。

参数

image

由图象创建函数(例如imagecreatetruecolor())返回的 GdImage 对象。

size

字体的尺寸,单位:点(磅)。

angle

以度为单位的角度,0 度表示从左到右阅读文本。较高的值表示逆时针旋转。例如,值为 90 将导致从下到上阅读文本。

x

xy 给出的坐标将定义第一个字符的基点(大致是字符的左下角)。 这与 imagestring() 不同,其中 xy 定义第一个字符的左上角。例如,“左上角”是 0, 0。

y

y 坐标。设置字体基线的位置,而不是字符的最底部。

color

文本所需颜色的索引,请参阅 imagecolorexact()

font_filename

希望使用的 TrueType 字体的路径。

根据 PHP 使用的 GD 库版本,font_filename 不以 / 开头时,将追加 .ttf 到文件名末尾,库将尝试沿着库定义的字体路径搜索该文件名。

在许多情况下,字体与使用的脚本位于同一目录中,以下技巧将缓解任何包含问题。

<?php
// 设置 GD 的环境变量
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>

text

要插入到图像中的文本。

options

options 可能的数组索引
类型 含义
linespacing float 定义绘制行距

返回值

这个函数返回数组,定义了盒子的四个点,从左下角开始逆时针移动:

0 左下 x 坐标
1 左下 y 坐标
2 右下 x 坐标
3 右下 x 坐标
4 右上 x 坐标
5 右上 x 坐标
6 左上 x 坐标
7 左上 y 坐标

失败时返回 false

更新日志

版本 说明
8.0.0 image 现在需要 GdImage 实例;之前需要有效的 gd resource

范例

示例 #1 imagefttext() 示例

<?php
// Create a 300x100 image
$im = imagecreatetruecolor(300, 100);
$red = imagecolorallocate($im, 0xFF, 0x00, 0x00);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);

// Make the background red
imagefilledrectangle($im, 0, 0, 299, 99, $red);

// Path to our ttf font file
$font_file = './arial.ttf';

// Draw the text 'PHP Manual' using font size 13
imagefttext($im, 13, 0, 105, 55, $black, $font_file, 'PHP Manual');

// Output image to the browser
header('Content-Type: image/png');

imagepng($im);
imagedestroy($im);
?>

注释

注意: 此函数仅在 PHP 编译时加入 freetype 支持时有效(--with-freetype-dir=DIR)。

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top