imagefttext

Write text to the image using fonts via FreeType 2

Syntax

imagefttext(
    GdImage $image,
    float $size,
    float $angle,
    int $x,
    int $y,
    int $color,
    string $font_filename,
    string $string,
    array $options = []
): array|false

Parameters

image

A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().

size

The font size in points.

angle

The angle in degrees.

Higher values represent a counter-clockwise rotation. 0 degrees reads left-to-right. 90 degrees reads bottom-to-top.

x

The x coordinate.

x and y define the lower-left corner of the first character. upper-left is 0,0.

y

The y coordinate.

Sets the position of the baseline, not the very bottom of the character.

color

The color index.

Using the negative of a color index has the effect of turning off antialiasing, see imagecolorallocate().

font_filename

The path to the TrueType font.

Depending on which version of the GD library PHP is using, when font_filename does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.

In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.

Note: Note that open_basedir does not apply to font_filename.

<?
// Set the environment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>
string

The string in UTF-8 encoding.

May include decimal numeric character references (of the form: &#8364;) to access characters in a font beyond position 127. The hexadecimal format (like &#xA9;) is supported. Strings in UTF-8 encoding can be passed directly.

Named entities, such as &copy;, are not supported. Consider using html_entity_decode() to decode these named entities into UTF-8 strings.

If a character is used in the string which is not supported by the font, a hollow rectangle will replace the character.

options

KeyTypeDescription
linespacingfloatDefines drawing linespacing

Return

Returns an array with 8 elements defining the four points of the bounding box of the text on success and false on error.

The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner seeing the text horizontally.

KeyDescription
0lower left x
1lower left y
2lower right x
3lower right y
4upper right x
5upper right y
6upper left x
7upper left y

Examples

1 · return

<?

$width = 100;
$height = 100;
$image = imagecreate($width, $height);

$red = 255;
$green = 255;
$blue = 255;
$color = imagecolorallocate($image, $red, $green, $blue);

$size = 8;
$angle = 0;
$x = 0;
$y = 0;
$font_filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/ttf/arial.ttf';
$string = "string";
$options = array(
    0
);

$return = imagefttext($image, $size, $angle, $x, $y, $color, $font_filename, $string, $options);

var_export($return);

?>
array (
  0 => 0,
  1 => 2,
  2 => 25,
  3 => 2,
  4 => 25,
  5 => -7,
  6 => 0,
  7 => -7,
)

2 · base64

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$red = 255;
$green = 255;
$blue = 255;
$color = imagecolorallocate($image, $red, $green, $blue);

$size = 8;
$angle = -45;
$x = 0;
$y = 7;
$font_filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/ttf/arial.ttf';
$string = "string";
$options = array(
    0
);
imagefttext($image, $size, $angle, $x, $y, $color, $font_filename, $string, $options);

ob_start();

    imagepng($image);

$output = ob_get_clean();

echo '<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,' . base64_encode($output) . '">
</body>
</html>';

?>
<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAIAAAD/gAIDAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACgklEQVR4nO3YMUgjQRTG8bkoKURCCCJiJSIiSwhiOYgsuAQViyAawmKRMpVayCJ2ipWCtVhZSBCxEUWChUUKsQgaJMgWwWJIkSJIEJGY6oqVkDvl4ODuZuX+v2p2qo/HvrczKwQAP+nr69Md4YvIZrNKqUKh0N/frzuLj3V2dgohms3m4ODg5ubmyclJIBCYmJhIp9O6o/nP4eGhYRjHx8dHR0dSyru7O9d1lVKjo6O6o/nP7e3t2NjYwMDA6+urlHJpaalWqz0+PobDYd3R/KVDCPH29razs3NzczM+Pl6v15eXl6enp8/OziYnJyuVysvLi+6QPmPbdi6X29/fb3Wf4zjPz8/b29utT6Q32vDu/v4+lUoJIRzHcV3Xsqzd3d2trS0hRDwe39vb0x1Qs2/tDysrK6FQqNFoJJNJy7Lq9bq3n8lkLi4unp6eaMkfSCkLhUL7aHccp1gsrq6ucv76RDAYbK29Sm1sbDQajfX19VAopDGYdoGPW81m01s4jmPb9unp6cLCwuLiYk9Pz/X1dXsp8U5K6b1T7V2plLIsS28wnzJNs71SiURCKdXV1aU3lX+1mm52dlYpZRiG3jxfgFepaDQqhOjt7T04OKhUKrlc7n/7pfPJgP9oaGhoZmamVCp1d3fn83mllGmaxWLRO6/ic2tra61zfDAYdF338vJSb6R/6feueyMjI1dXV956amoqHA7Pz8//hVRfnG3bhmEkEgnXdePxeCaTKZfL3hTDz6LRqFIqFouZpnl+fl6tVqnUr8zNzdVqtXw+//DwMDw8rDuO70UiESklNx4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP6o7+iD6Mb25uk/AAAAAElFTkSuQmCC">
</body>
</html>

gd_info

getimagesize

getimagesizefromstring

image_type_to_extension

image_type_to_mime_type

imageaffine

imageaffinematrixconcat

imageaffinematrixget

imagealphablending

imageantialias

imagearc

imageavif

imagebmp

imagechar

imagecharup

imagecolorallocate

imagecolorallocatealpha

imagecolorat

imagecolorclosest

imagecolorclosestalpha

imagecolorclosesthwb

imagecolordeallocate

imagecolorexact

imagecolorexactalpha

imagecolormatch

imagecolorresolve

imagecolorresolvealpha

imagecolorset

imagecolorsforindex

imagecolorstotal

imagecolortransparent

imageconvolution

imagecopy

imagecopymerge

imagecopymergegray

imagecopyresampled

imagecopyresized

imagecreate

imagecreatefromavif

imagecreatefrombmp

imagecreatefromgif

imagecreatefromjpeg

imagecreatefrompng

imagecreatefromstring

imagecreatefromtga

imagecreatefromwbmp

imagecreatefromwebp

imagecreatefromxbm

imagecreatefromxpm

imagecreatetruecolor

imagecrop

imagecropauto

imagedashedline

imagedestroy

imageellipse

imagefill

imagefilledarc

imagefilledellipse

imagefilledpolygon

imagefilledrectangle

imagefilltoborder

imagefilter

imageflip

imagefontheight

imagefontwidth

imageftbbox

imagegammacorrect

imagegetclip

imagegetinterpolation

imagegif

imageinterlace

imageistruecolor

imagejpeg

imagelayereffect

imageline

imageloadfont

imageopenpolygon

imagepalettecopy

imagepalettetotruecolor

imagepng

imagepolygon

imagerectangle

imageresolution

imagerotate

imagesavealpha

imagescale

imagesetbrush

imagesetclip

imagesetinterpolation

imagesetpixel

imagesetstyle

imagesetthickness

imagesettile

imagestring

imagestringup

imagesx

imagesy

imagetruecolortopalette

imagettfbbox

imagettftext

imagetypes

imagewbmp

imagewebp

imagexbm

iptcembed

iptcparse

HomeMenu