imagettftext
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.
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(400, 40);
$bkg_color = imagecolorallocate($id_01, 255, 0, 0);
$bdr_color = imagecolorallocate($id_01, 0, 0, 0);
$txt_color = imagecolorallocate($id_01, 255, 255, 255);
$ttf_arr =
imagettftext($id_01, $font_size, $angle_txt,
$pos_x, $pos_y, $txt_color, $font_name, $txt );
imagerectangle ($id_01, 1, 1, 399, 39, $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.pngABOUT THE EMBEDED TEXT RECTANGLE |
COORDINATES | MEANING |
112 | x-coordinate LOWER LEFT |
33 | y-coordinate LOWER LEFT |
311 | x-coordinate LOWER RIGHT |
33 | y-coordinate LOWER RIGHT |
311 | x-coordinate TOP LEFT |
10 | y-coordinate TOP LEFT |
112 | x-coordinate TOP RIGHT |
10 | y-coordinate TOP RIGHT |
ed48 |
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_aimgfile, 255, 255, 0);
$pol_color =
imagecolorallocatealpha($id_aimgfile, 0, 0, 0, 50);
$pol_line = imagecolorallocate($id_aimgfile, 0, 0, 0);
$ttf_arr =
imagettftext($id_aimgfile, $font_size, $angle_en,
$pos_x, $pos_y, $en_color, $font_name, $$lang );
imagepolygon($id_aimgfile, $ttf_arr, 4, $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.pngPNG 090 02 imagettftext.png