What are the best practices for serving images using PHP?

When serving images using PHP, it is important to set the appropriate headers to ensure the image is displayed correctly in the browser. This includes setting the content type header to specify the image type and using readfile() function to output the image data.

<?php
// Specify the image file path
$imagePath = 'path_to_image.jpg';

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

// Output the image data
readfile($imagePath);