How does “imagettfbbox()” in PHP work?

前端 未结 3 915
悲哀的现实
悲哀的现实 2020-12-18 22:24

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

imagettfbbox() returns an array with 8 el

3条回答
  •  青春惊慌失措
    2020-12-18 23:27

    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.

提交回复
热议问题