How can you properly set the Content-Type header when serving images in PHP to ensure they are displayed correctly?

When serving images in PHP, it is important to set the Content-Type header correctly to ensure that the browser knows how to display the image. This can be done by using the header() function in PHP to set the Content-Type to the appropriate image type, such as "image/jpeg" for JPEG images or "image/png" for PNG images.

// Set the Content-Type header based on the image type
header('Content-Type: image/jpeg');

// Output the image data
readfile('path/to/image.jpg');