首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

imagettfbbox

(PHP 4, PHP 5, PHP 7)

imagettfbbox - 使用TrueType字体给出文本的边界框

描述

代码语言:javascript
复制
array imagettfbbox ( float $size , float $angle , string $fontfile , string $text )

此函数计算并返回TrueType文本的像素边界框。

参数

size

字体大小。

注意:在GD 1中,这是以像素为单位测量的。在GD 2中,这是以点数衡量的。

angle

将度量文本的度数角度。

fontfile

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

根据PHP所使用的GD库的版本,当fontfile不以前导符/ /开头时,.ttf将被附加到文件名中,并且库将尝试沿着库定义的字体路径搜索该文件名。

当使用低于2.0.18的GD库版本时,空格字符而不是分号被用作不同字体文件的'路径分隔符'。无意中使用此功能将导致警告消息:警告:无法找到/打开字体。对于这些受影响的版本,唯一的解决方案是将字体移动到不包含空格的路径。

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

代码语言:javascript
复制
<?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

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

注意:注意的open_basedir不能 适用于fontfile

text

要测量的字符串。

返回值

imagettfbbox()返回一个数组,其中8个元素表示四个点,使得文本的边界框成功,错误时为FALSE。

内容

0

左下角,X位置

1

左下角,Y位置

2

右下角,X位置

3

右下角,Y位置

4

右上角,X位置

5

右上角,Y位置

6

左上角,X位置

7

左上角,Y位置

无论角度如何,点是相对于文本的,所以“左上角”表示在左上角水平地看文本。

例子

示例#1 imagettfbbox()示例

代码语言:javascript
复制
<?php
// Create a 300x150 image
$im = imagecreatetruecolor(300, 150);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

// Set the background to be white
imagefilledrectangle($im, 0, 0, 299, 299, $white);

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

// First we create our bounding box for the first text
$bbox = imagettfbbox(10, 45, $font, 'Powered by PHP ' . phpversion());

// This is our cordinates for X and Y
$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) - 25;
$y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5;

// Write it
imagettftext($im, 10, 45, $x, $y, $black, $font, 'Powered by PHP ' . phpversion());

// Create the next bounding box for the second text
$bbox = imagettfbbox(10, 45, $font, 'and Zend Engine ' . zend_version());

// Set the cordinates so its next to the first text
$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) + 10;
$y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5;

// Write it
imagettftext($im, 10, 45, $x, $y, $black, $font, 'and Zend Engine ' . zend_version());

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

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

注意

注意:只有PHP编译时支持freetype(-- with-freetype-dir = DIR)才能使用该函数。

扩展内容

  • imagettftext() - 使用TrueType字体将文本写入图像
  • imageftbbox() - 通过freetype2使用字体给出文本的边界框

← imagetruecolortopalette

imagettftext →

扫码关注腾讯云开发者

领取腾讯云代金券