How does "imagettfbbox ()" work in PHP?

Could you explain what exactly the imagettfbbox() return value means? The manual states :

imagettfbbox () returns an array with 8 elements representing four points that make the bounding box of the text successful, and FALSE is an error. [... Table of points here ...] Points refer to the text regardless of the angle, so "upper left" means that the horizontal text is displayed in the upper left corner.

But I found this 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 are there negative values?

+7
source share
2 answers

You should see the comment from "marclaz" on the imagettfbbox man page :

Please note that since the imageTTFBbox and imageTTFText functions return an array of coordinates, which can be a negative number, must be taken with height and width calculations.

The correct way to do this is to use the abs () function:

for horizontal text:

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

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

 $x -= $width/2; $y += $heigth/2; imageTTFText($img,$size,0,$x,$y,$color,$font,$text); 

because (0,0) the top of the page is the top corner of the page and (0,0) the origin text is the bottom left readable text corner.

+9
source

The following resource explains this: http://www.tuxradar.com/practicalphp/11/2/6

Just use abs () . This is from the resource above: "[function] returns its values ​​from the lower left corner of the baseline of the text string, and not to the absolute lower left corner. The baseline of the letter is the place where it will sit if you were to write it on the aligned paper "

+1
source

All Articles