Friday, October 24, 2025
HomeLanguagesPHP | imageftbbox() Function

PHP | imageftbbox() Function

The imageftbbox() function is an inbuilt function in PHP which is used to calculate the bounding box of a text using fonts via freetype2.

Syntax:

array imageftbbox( float $size, array $angle,
          string $fontfile, string $text, array $extrainfo )

Parameters: This function accept five parameters as mentioned above and described below:

  • $size: It specifies the font size in points.
  • $angle: It specifies the angle in degrees in which text will be measured.
  • $fontfile: It specifies the font filename.
  • $text: It specifies the string to be measured.
  • $extrainfo (Optional): It specifies the extra information.

Return Value: This function returns an array on success.

Below given programs illustrate the imageftbbox() function in PHP:

Program 1:




<?php
  
// Create bounding box with local font file
$bbox = imageftbbox(100, 100, './Pacifico.ttf', 'neveropen');
  
// Print the boundbox data
print("<pre>".print_r($bbox, true)."</pre>");
?>


Output:

Array
(
    [0] => 47
    [1] => -13
    [2] => -91
    [3] => -806
    [4] => -264
    [5] => -776
    [6] => -124
    [7] => 17
)

Program 2:




<?php
  
// Create an image
$im = imagecreatetruecolor(800, 250);
  
// Set the background to be light blue
imagefilledrectangle($im, 0, 0, 299, 299,
       imagecolorallocate($im, 0, 0, 100));
  
// Create bounding box with local font file
$bbox = imageftbbox(10, 0, './Pacifico.ttf',
            'neveropen');
  
// Calculate coordinates using bounding box
$x = $bbox[0] + 130;
$y = $bbox[1] + 130;
  
// Add text
imagefttext($im, 50, 0, $x, $y, imagecolorallocate($im,
         0, 150, 0), './Pacifico.ttf', 'neveropen');
  
// Output to browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>


Output:

Reference: https://www.php.net/manual/en/function.imageftbbox.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS