Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

gd - How does "imagettfbbox()" in PHP work?

Could you please explain what exactly the return value of imagettfbbox() mean? 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.

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

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

means the following points:

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

How should I interpret them?

Why there are negative values?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should take a look at the 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...