PHP | imagettfbbox() Function
Last Updated :
01 Jun, 2021
The imagettfbbox() function is an inbuilt function in PHP that is used to calculate the bounding box in pixels for a TrueType text.
Syntax:
array imagettfbbox( float $size, float $angle,
string $fontfile, string $text)
Parameters: This function accepts four 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.
Return Value: This function returns an array on success.
Below examples illustrate the imagettfbbox() function in PHP:
Example 1:
php
<?php
$bbox = imagettfbbox(100, 100,
'./Pacifico.ttf' , 'GeeksforGeeks' );
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
)
Example 2:
php
<?php
$im = imagecreatetruecolor(800, 250);
imagefilledrectangle( $im , 0, 0, 800, 299,
imagecolorallocate( $im , 255, 0, 100));
$bbox = imagettfbbox(10, 0,
'./Pacifico.ttf' , 'GeeksforGeeks' );
$x = $bbox [0] + 130;
$y = $bbox [1] + 130;
imagettftext( $im , 50, 0, $x , $y ,
imagecolorallocate( $im , 0, 150, 0),
'./Pacifico.ttf' , 'GeeksforGeeks' );
header( 'Content-Type: image/png' );
imagepng( $im );
imagedestroy( $im );
?>
|
Output:
Reference: https://www.php.net/manual/en/function.imagettfbbox.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...