What are some common pitfalls to avoid when trying to display PHP function results in images?

One common pitfall to avoid when trying to display PHP function results in images is not properly setting the content type header to indicate that the output is an image. This can result in the image not being displayed correctly or at all. To solve this issue, make sure to set the content type header to "image/jpeg" or "image/png" before outputting the image data.

header('Content-Type: image/jpeg');

// Your PHP function to generate or retrieve image data
$imageData = generateImageData();

// Output the image data
echo $imageData;