imagettfbbox 


gd apg

CALCULATES and returns the bounding box in pixels for a TrueType text.





Check your version of GD, as versions prior to 2.0.18 may produce an error when making a $font_filename call.

The version of GD in use can be viewed by running the gd_info function.



<?php

array|false imagettfbbox ( float $size , 
                                          
float $angle, 
                                        
string $font_filename, 
                                        
string $string,
                                        array 
$options = [] )

where,

$size = The font size in points

$angle 
= The angle in degrees to measure the text

$font_filename 
= The path to the TrueType font you wish to use

$string = The text STRING to be measured

$options 
= Options for written

?>
 

  $size   


The font size in points.



  $angle   


The angle in degrees in which text will be measured.



  $font_filename   


The path to the TrueType font you wish to use.



  $string   


The text string to be measured.



  $options   


The options for written.

Since PHP 8.0.0.



  1 EXERCISE   

<?php

echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';

if(
PHP_MAJOR_VERSION < 8)
{
$var = getcwd();

$font_name = "$var/ttf/VeraMoIt.ttf";
// complete path
}
else
{
$font_name = "ttf/VeraMoIt.ttf";
} 

$font_size = 16;
$angle_txt = 23;
$pos_x = 49;
$pos_y = 196;
$txt = 'Anyway ... we were found!';

$d_base_img = 'png/baseXx.png';

$d_dest_img = 'png/PNG 092 01imagettfbbox.png';

$id_dimgfile = imagecreatefrompng($d_base_img);

$pol_line  = imagecolorallocate($id_dimgfile, 255, 0, 255); 

imagepng($id_dimgfile, $d_base_img);

echo 
$d_base_img; ?><br><br>
<img src="<?php echo $d_base_img; ?>"
alt="<?php echo $d_base_img; ?> app" title="<?php echo $d_base_img; ?>"><br><br><br> 

<?php

$txt_color 
= imagecolorallocate($id_dimgfile, 255, 0, 0);

$ttf_arr = imagettftext($id_dimgfile, $font_size, $angle_txt, 
$pos_x, $pos_y, $txt_color, $font_name, $txt );

$ttfdbox_arr = imagettfbbox($font_size, $angle_txt, $font_name, $txt);

imagepolygon($id_dimgfile, $ttf_arr, 4, $pol_line);

imagepng($id_dimgfile, $d_dest_img);

echo 
$d_dest_img; ?><br><br>
<img src="<?php echo $d_dest_img; ?>"
alt="<?php echo $d_dest_img; ?> app" title="<?php echo $d_dest_img; ?>">

<?php
echo '<br><br>THE BOUNDING BOX OF TEXT:<pre>';
print_r($ttfdbox_arr);
echo 
'</pre><br>';

echo 
'<br>THE VERTICES OF THE VIRTUAL RECTANGLE OF TEXT:<pre>';
print_r($ttf_arr);
echo 
'</pre><br>'; 

 RESULT   

baseXx.png

baseXx.png apr


PNG 092 01imagettfbbox.png

PNG 092 01imagettfbbox.png apr

THE BOUNDING BOX OF TEXT
keyCOORDINATEMEANING
0-1x-coordinate LOWER LEFT
15y-coordinate LOWER LEFT
2298x-coordinate LOWER RIGHT
3-121y-coordinate LOWER RIGHT
4290x-coordinate TOP LEFT
5-141y-coordinate TOP LEFT
6-9x-coordinate TOP RIGHT
7-14y-coordinate TOP RIGHT
ed48

THE VERTICES OF
THE VIRTUAL RECTANGLE OF TEXT
keyCOORDINATEMEANING
048x-coordinate LOWER LEFT
1201y-coordinate LOWER LEFT
2347x-coordinate LOWER RIGHT
375y-coordinate LOWER RIGHT
4339x-coordinate TOP LEFT
555y-coordinate TOP LEFT
640x-coordinate TOP RIGHT
7182y-coordinate TOP RIGHT
ed48