请您解释一下 imagettfbbox()
的返回值到底是什么意思? The manual says :
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.
但是,我发现不是很清楚。例如返回值:
array(-1, 1, 61, 1, 61, -96, -1, -96)
表示以下几点:
(-1, -96) ------ (61, -96)
| |
| |
| |
| |
| |
| |
(-1, 1) -------- (61, 1)
我应该如何解释它们?
为什么会有负值?
最佳答案
你应该看看 comment by "marclaz" on the imagettfbbox
manual page :
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.
The rigth way to do that is to use the abs() function:
for an horizontal text:
$box = @imageTTFBbox($size,0,$font,$text); $width = abs($box[4] - $box[0]); $height = abs($box[5] - $box[1]);
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);
this because (0,0) page origin is topleft page corner and (0,0) text origin is lower-left readable text corner.
关于php - PHP 中的 "imagettfbbox()"是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338072/