What best practices should be followed when offering image downloads on a website using PHP?

When offering image downloads on a website using PHP, it is important to set the appropriate headers to ensure the image is downloaded correctly by the browser. This includes setting the content type to 'image/jpeg' or 'image/png' depending on the image type, and using the 'Content-Disposition' header with a 'attachment' value to prompt the browser to download the file instead of displaying it in the browser window.

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

// Set the appropriate headers
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="image.jpg"');

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