imagefttext 


gd apg

WRITES a given STRING text into one image using TrueType fonts at a given position in an image file and return an ARRAY with the approximate coordinates of the vertices of the virtual rectangle surrounding the written STRING.





This function is an extended variant of imagettftext function which additionally supports the $options.

One of possible ARRAY index for $options is linespacing that is a FLOAT value wich defines the drawing linespacing.

$image is an image resource, returned by one of the image creation functions, such as imagecreatetruecolor.

If $angle = 0 the $text will be set left-to-right reading text.

Higher values of $angle represent a counterclockwise rotation.

For example a value $angle = 90 would result in bottom-to-top reading text.

The coordinates $x and $y will define the basepoint of the first character, that is, roughly the lower-left corner of the character.

It is worth noting that, this introduces a difference from the imagestring function, where, where $x = 0 and $y = 0 define the upper-left or top-left, corner of the first character.

The $y sets the position of the fonts baseline, not the very bottom of the character.

If $color < 0, the antialias effect will be turning off.

This function is only available if PHP is compiled with freetype support.

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 imagefttext ( GdImage $image, 
                                             
float $size, 
                                             
float $angle, 
                                               
int $x, 
                                               
int $y, 
                                               
int $color, 
                                          
string $font_filename, 
                                          
string $text, 
                                           array 
$options = [] )

where,

$image = An image identifier

$size 
= The font size in points

$angle 
= The angle in degrees to be written in the image

$x 
= The x-coordinate where the 
                           first character will be written

$y 
= The y-coordinate where the 
                           first character will be written

$color 
= The color index to be used

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

$text = The text STRING - UTF-8 encoded

$options 
= The extended variant of 
                          imagettftext which additionally support

?>
 

  $image   


An image identifier.



  $size   


The font size in points.



  $angle   


The angle in degrees to be written in the image.



  $x   


The x-coordinate of the image where the first character will be written.



  $y   


The y-coordinate of the image where the first character will be written.



  $color   


The color index to be used.



  $font_filename   


The path to the TrueType font you wish to use.



  $text   


The text string encoded in UTF-8.



  $options   


This parameter adds extended support for the imagettftext function.



  1 EXERCISE   

<?php

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

$lang = 'pt';
// $lang = 'en';

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

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

$font_size = 20;
$angle_txt = 0;
$pos_x = 30;
$pos_y = 300;
$txt = 'no & zerof - Portugal 2018';

$b_base_img = 'jpeg/JPEG 040 01.jpg';

$b_dest_img = 'png/PNG 091 01 imagefttext.png';

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

$id_bimgfile 
= imagecreatefromjpeg($b_base_img);

$txt_color = imagecolorallocate($id_bimgfile, 255, 255, 255);
$lin_color = imagecolorallocate($id_bimgfile, 255, 0, 0);

$ttf_arr = imagefttext($id_bimgfile, $font_size, $angle_txt, 
$pos_x, $pos_y, $txt_color, $font_name, $txt );
imageantialias($id_bimgfile, 1);

imagepng($id_bimgfile, $b_dest_img);

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

<table width="100%" cellspacing="5" 
cellpadding="5" border="1"><tbody>
<tr><td colspan="2">EMBEDED TEXT RECTANGLE</td></tr>
<tr> <td>COORDINATES</td><td>MEANING</td></tr>    

<?php

foreach ($ttf_arr as $ar => $arr)
{
$arr_ndx = [ 
'x-coordinate LOWER LEFT', 
'y-coordinate LOWER LEFT',
'x-coordinate LOWER RIGHT', 
'y-coordinate LOWER RIGHT',              
'x-coordinate TOP LEFT', 
'y-coordinate TOP LEFT',
'x-coordinate TOP RIGHT', 
'y-coordinate TOP RIGHT' 
];

echo 
'<tr><td>' . $arr . '</td><td>' . $arr_ndx[$ar] . '</td></tr>';
}

?>
<tr><td colspan="2">ed48</td></tr></tbody></table>


 RESULT   

JPEG 040 01.jpg

JPEG 040 01.jpg apr

PNG 091 01 imagefttext.png

PNG 091 01 imagefttext.png apr

ABOUT THE EMBEDED TEXT RECTANGLE
COORDINATESMEANING
30x-coordinate LOWER LEFT
306y-coordinate LOWER LEFT
390x-coordinate LOWER RIGHT
306y-coordinate LOWER RIGHT
390x-coordinate TOP LEFT
279y-coordinate TOP LEFT
30x-coordinate TOP RIGHT
279y-coordinate TOP RIGHT
ed48