What are the best practices for handling image output in PHP to ensure compatibility and error-free display across different browsers and platforms?

When handling image output in PHP, it is important to use the correct content-type header to ensure compatibility and error-free display across different browsers and platforms. This can be achieved by setting the header to 'Content-Type: image/jpeg' or 'Content-Type: image/png' depending on the image format. Additionally, it is recommended to use functions like imagejpeg() or imagepng() to output the image data.

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

// Output the JPEG image
$image = imagecreatefromjpeg('image.jpg');
imagejpeg($image);
imagedestroy($image);