imagettftext 


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.





$image is an image 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.

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.

The $options has been added in PHP 8.0.0.



<?php

array|false imagettftext 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 
Options for written


?>
 

  $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.



  $optins   


The options for written.

Since PHP 8.0.0.



  1 EXERCISE   

<?php

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

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

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

$font_size 18;
$angle_txt 0;
$pos_x 110;
$pos_y 28;
$txt '+ Alea jacta est!';

$file01 'png/PNG 089 01.png';

$id_01 imagecreate(40040);
$bkg_color imagecolorallocate($id_0125500);
$bdr_color imagecolorallocate($id_01000);
$txt_color imagecolorallocate($id_01255255255);

$ttf_arr 
imagettftext($id_01$font_size$angle_txt
$pos_x$pos_y$txt_color$font_name$txt );

imagerectangle ($id_011139939$bdr_color);

imagepng($id_01$file01);

echo 
$file01?><br><br>
<img src="<?php echo $file01?>"
alt="<?php echo $file01?>
title="<?php echo $file01?>"><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   

PNG 089 01.png

PNG 089 01.png apr

ABOUT THE EMBEDED TEXT RECTANGLE
COORDINATESMEANING
112x-coordinate LOWER LEFT
33y-coordinate LOWER LEFT
311x-coordinate LOWER RIGHT
33y-coordinate LOWER RIGHT
311x-coordinate TOP LEFT
10y-coordinate TOP LEFT
112x-coordinate TOP RIGHT
10y-coordinate TOP RIGHT
ed48


  2 EXERCISE   

<?php

// $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 16;
$angle_en 55;
$pos_x 210;
$pos_y 220;
$pt ' Tecelã Moderna ';
$en ' Modern Weaver ';

$a_base_img 'png/PNG 090 02.png';

$a_dest_img 'png/PNG 090 02 imagettftext.png';

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

$id_aimgfile 
imagecreatefrompng($a_base_img);

$en_color imagecolorallocate($id_aimgfile2552550);
$pol_color 
imagecolorallocatealpha($id_aimgfile00050);
$pol_line  imagecolorallocate($id_aimgfile000);

$ttf_arr 
imagettftext($id_aimgfile$font_size$angle_en
$pos_x$pos_y$en_color$font_name, $$lang );

imagepolygon($id_aimgfile$ttf_arr4$pol_line);

imagepng($id_aimgfile$a_dest_img);

imagedestroy($id_aimgfile);

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


 RESULT   

PNG 090 02.png

PNG 090 02.png apr

PNG 090 02 imagettftext.png

PNG 090 02 imagettftext.png apr