How can the readfile() function be used to include images in PHP with the appropriate content-type?

When including images in PHP using the readfile() function, it is important to set the appropriate content-type header to ensure the browser interprets the data correctly. This can be achieved by setting the content-type header to the image type before reading and outputting the image file using readfile(). This ensures that the browser knows how to display the image content.

<?php
$imagePath = 'path/to/image.jpg';

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

// Output the image file using readfile()
readfile($imagePath);
?>