How can the header function be used to properly display an image in PHP?

To properly display an image in PHP using the header function, you need to set the content type header to 'image/jpeg', 'image/png', or 'image/gif' based on the image type. Then, read the image file using functions like file_get_contents and output it using echo. Finally, make sure there is no other output before or after the image data to prevent any issues with the image display.

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

// Read the image file
$image = file_get_contents('path/to/image.jpg');

// Output the image
echo $image;