PHP | imagettftext() Function

The imagettftext() function is an inbuilt function in PHP which is used to write text to the image using TrueType fonts.

Syntax:

array imagettftext( resource $image, float $size, float $angle,
         int $x, int $y, int $color, string $fontfile, string $text)

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

  • $image: It specifies the image to be worked upon.
  • $size: It specifies the font size to use in points.
  • $angle: It specifies the angle in degrees.
  • $x: It specifies the x-coordinate.
  • $y: It specifies the y-coordinate.
  • $color: It specifies the index of the desired color for the text.
  • $fontfile: It specifies the font to be used.
  • $text: It specifies the text to write.

Return Value: This function returns an array on success.

Below examples illustrates the imagettftext() function in PHP.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create an empty image
$im = imagecreatetruecolor(800, 250);
  
// Add text using a font from local file
$dataArr = imagettftext($im, 0, 0, 10, 10,
        imagecolorallocate($im, 0, 150, 0),
        './Pacifico.ttf', 'GeeksforGeeks');
  
// Output to browser
print("<pre>" . print_r($dataArr, true) . "</pre>");
?>

chevron_right


Output:

Array
(
    [0] => 8
    [1] => 12
    [2] => 18
    [3] => 12
    [4] => 18
    [5] => 7
    [6] => 8
    [7] => 7
)

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create an empty image
$im = imagecreatetruecolor(800, 250);
  
// Add text using a font from local file
imagettftext($im, 90, 0, 100, 100, 
            imagecolorallocate($im, 0, 0, 255), 
            './RugeBoogie-Regular.ttf', 'GeeksforGeeks');
  
// Output to browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>

chevron_right


Output:

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




My Personal Notes arrow_drop_up

I love Open Source Culture have contributed to phpMyAdmin, LibreOffice, Ant-Design and Web is my passion

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.