imageftbbox()函数是PHP中的内置函数,用于通过freetype2使用字体来计算文本的边界框。
用法:
array imageftbbox( float $size, array $angle,
string $fontfile, string $text, array $extrainfo )
参数:该函数接受上述和以下所述的五个参数:
$size:它以磅为单位指定字体大小。
$angle:它指定以度为单位的角度,在该角度下将测量文本。
$fontfile:它指定字体文件名。
$text:它指定要测量的字符串。
$extrainfo (Optional):它指定了额外的信息。
返回值:该函数成功返回一个数组。
下面给出的程序说明了PHP中的imageftbbox()函数:
程序1:
// Create bounding box with local font file
$bbox = imageftbbox(100, 100, './Pacifico.ttf', 'GeeksforGeeks');
// Print the boundbox data
print("
".print_r($bbox, true)."");
?>
输出:
Array
(
[0] => 47
[1] => -13
[2] => -91
[3] => -806
[4] => -264
[5] => -776
[6] => -124
[7] => 17
)
程序2:
// Create an image
$im = imagecreatetruecolor(800, 250);
// Set the background to be light blue
imagefilledrectangle($im, 0, 0, 299, 299,
imagecolorallocate($im, 0, 0, 100));
// Create bounding box with local font file
$bbox = imageftbbox(10, 0, './Pacifico.ttf',
'GeeksforGeeks');
// Calculate coordinates using bounding box
$x = $bbox[0] + 130;
$y = $bbox[1] + 130;
// Add text
imagefttext($im, 50, 0, $x, $y, imagecolorallocate($im,
0, 150, 0), './Pacifico.ttf', 'GeeksforGeeks');
// Output to browser
header('Content-Type:image/png');
imagepng($im);
imagedestroy($im);
?>
输出: