What is the function imagettftext in PHP used for?

The function imagettftext in PHP is used to draw text on an image using a TrueType font. This function allows you to specify the font size, angle, color, and position of the text on the image. It is commonly used in image manipulation tasks such as creating dynamic images with text overlays or watermarking images with text.

<?php
// Create a blank image with a specified width and height
$image = imagecreatetruecolor(400, 200);

// Set the background color of the image
$bg_color = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bg_color);

// Set the text color
$text_color = imagecolorallocate($image, 0, 0, 0);

// Set the TrueType font file path
$font = 'arial.ttf';

// Add text to the image
imagettftext($image, 20, 0, 50, 100, $text_color, $font, 'Hello, World!');

// Output the image to the browser
header('Content-Type: image/png');
imagepng($image);

// Free up memory
imagedestroy($image);
?>