"imagettfbbox()"如何在PHP工作? [英] How does "imagettfbbox()" in PHP work?

查看:103
本文介绍了"imagettfbbox()"如何在PHP工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您解释imagettfbbox()的返回值到底是什么意思? 手册说:

Could you please explain what exactly the return value of imagettfbbox() mean? The manual says:

imagettfbbox()返回一个包含8个元素的数组,这些元素代表四个 点使成功的文本成为边界框,而使成功的则为FALSE 错误. [...点数表...] 无论角度如何,这些点都是相对于文本的,因此上 左"表示在左上角水平查看文本.

imagettfbbox() returns an array with 8 elements representing four points making the bounding box of the text on success and FALSE on error. [...Table of points here...] The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner seeing the text horizontally.

但是,我发现它不是很清楚.例如,返回值:

But, I found it not very clear. For example, the return value:

array(-1, 1, 61, 1, 61, -96, -1, -96)

表示以下几点:

(-1, -96) ------ (61, -96)
    |                |
    |                |
    |                |
    |                |
    |                |
    |                |
 (-1, 1) -------- (61, 1)              

我应该如何解释它们?

How should I interpret them?

为什么会有负值?

推荐答案

您应该看看 :

You should take a look at the comment by "marclaz" on the imagettfbbox manual page :

请注意,当imageTTFBbox和imageTTFText函数返回一个 可能为负数的坐标数组,必须注意 进行高度和宽度计算.

Please note that as imageTTFBbox and imageTTFText functions return an array of coordinates which could be negative numbers care must be taken with height and width calculations.

做到这一点的最严格方法是使用abs()函数:

The rigth way to do that is to use the abs() function:

对于水平文本:

$box = @imageTTFBbox($size,0,$font,$text); $width = abs($box[4] -
$box[0]); $height = abs($box[5] - $box[1]);

然后将文本居中放置在($ x,$ y)位置,代码应类似于

Then to center your text at ($x,$y) position the code should be like that:

$x -= $width/2; $y += $heigth/2;

imageTTFText($img,$size,0,$x,$y,$color,$font,$text);

这是因为(0,0)页面原点是左上角和(0,0)文本 原点是左下可读文本角.

this because (0,0) page origin is topleft page corner and (0,0) text origin is lower-left readable text corner.

这篇关于"imagettfbbox()"如何在PHP工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆