How can one display a custom error message instead of the default broken image icon when an error occurs in a PHP-generated image?

When an error occurs in a PHP-generated image, instead of displaying the default broken image icon, you can display a custom error message by using the `header()` function to set the content type to text and then outputting the error message using `echo`.

<?php
// Generate image
$image = imagecreate(200, 200);

if (!$image) {
    // Output custom error message
    header('Content-Type: text/plain');
    echo 'Error generating image';
} else {
    // Output image
    header('Content-Type: image/png');
    imagepng($image);
    imagedestroy($image);
}
?>