PHP 8.5.0 Alpha 4 available for testing

Voting

: six plus two?
(Example: nine)

The Note You're Voting On

Nate Sweet
18 years ago
It took me some time to make full use of imagettfbbox. Hopefully the following function makes it much easier to use for others.

<?php
function convertBoundingBox ($bbox) {
if (
$bbox[0] >= -1)
$leftOfBasepoint = -abs($bbox[0] + 1);
else
$leftOfBasepoint = abs($bbox[0] + 2);
$rightOfBasepoint = abs($bbox[2] - $bbox[0]);
if (
$bbox[0] < -1) $rightOfBasepoint = abs($bbox[2]) + abs($bbox[0]) - 1;
$aboveBasepoint = abs($bbox[5] + 1);
$height = abs($bbox[7]) - abs($bbox[1]);
if (
$bbox[3] > 0) $height = abs($bbox[7] - $bbox[1]) - 1;
$width = $leftOfBasepoint + $rightOfBasepoint;
$belowBasepoint = $height - $aboveBasepoint;
return array(
'width' => $width,
'height' => $height,
'leftOfBasepoint' => $leftOfBasepoint,
'rightOfBasepoint' => $rightOfBasepoint,
'aboveBasepoint' => $aboveBasepoint,
'belowBasepoint' => $belowBasepoint
);
}
?>

Thanks goes to mike at mikeleigh dot com for providing the core of this function.

Remember, the basepoint is the x, y coords you use to draw text with imagettftext. A useful thing to do is take a string like...
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890
...and use the "aboveBasepoint" value for the height of your font. Now you can draw lines and use "the height of your font * leading" as the distance between lines of text, where leading is a number like 1.45 (for 45% leading).

<< Back to user notes page

To Top