Monday, December 22, 2025
HomeLanguagesPHP | imagefttext() Function

PHP | imagefttext() Function

The imagefttext() function is an inbuilt function in PHP which is used write text to the image using fonts using FreeType 2.

Syntax:

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

Parameters: This function accept nine 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.
  • $extrainfo (Optional): It specifies the extra information.

Return Value: This function returns an array on success.

Below examples illustrates the imagefttext() function in PHP.

Example 1:




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


Output:

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

Program 2:




<?php
  
// Create an empty image
$im = imagecreatetruecolor(800, 250);
  
// Add text using a font from local file
imagefttext($im, 50, 0, 100, 100, 
            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.imagefttext.php

RELATED ARTICLES

Most Popular

Dominic
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS