What are some common pitfalls for beginners in PHP when trying to add text to an image?

One common pitfall for beginners in PHP when trying to add text to an image is not specifying the correct font file path. Make sure to provide the correct path to the font file on your server to avoid any errors. Additionally, be sure to set the correct image type and color for the text to be visible on the image.

// Specify the correct font file path
$font = 'path/to/font.ttf';
// Set the image type and color
header('Content-Type: image/png');
$im = imagecreatefrompng('image.png');
$black = imagecolorallocate($im, 0, 0, 0);
// Add text to the image
imagettftext($im, 20, 0, 10, 50, $black, $font, 'Hello World');
// Output the image
imagepng($im);
imagedestroy($im);