函数名称:imagefontheight()
函数描述:该函数用于获取指定字体的高度。
参数:无
返回值:返回指定字体的高度,单位为像素。
适用版本:该函数适用于所有PHP版本。
示例:
// 创建一个画布
$image = imagecreatetruecolor(200, 100);
// 定义字体文件路径
$fontFile = 'arial.ttf';
// 设置字体大小和颜色
$fontSize = 20;
$fontColor = imagecolorallocate($image, 255, 255, 255);
// 设置背景颜色
$backgroundColor = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $backgroundColor);
// 获取字体高度
$fontHeight = imagefontheight($fontSize);
// 在画布上写入文本
$text = "Hello World";
$x = 10;
$y = ($fontHeight + 100) / 2;
imagettftext($image, $fontSize, 0, $x, $y, $fontColor, $fontFile, $text);
// 输出图像
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
以上示例中,我们首先创建一个200x100像素的画布,然后定义了一个字体文件路径和字体大小和颜色。接着我们设置了画布的背景颜色,并使用imagefill函数将整个画布填充为黑色。然后,通过调用imagefontheight函数获取字体的高度,以便在后续的imagettftext函数中正确定位文本的位置。最后,通过imagettftext函数在画布上写入了"Hello World"文本,并将图像输出为PNG格式。