What are some common pitfalls when trying to display images in PHP, and how can they be avoided?

One common pitfall when displaying images in PHP is not setting the correct content type header before outputting the image. This can result in the image not being displayed correctly in the browser. To avoid this issue, make sure to set the content type header to 'image/jpeg', 'image/png', or 'image/gif' depending on the image type.

<?php
// Set the content type header
header('Content-Type: image/jpeg');

// Output the image
$image = imagecreatefromjpeg('image.jpg');
imagejpeg($image);
imagedestroy($image);
?>