How can the header function in PHP affect the output of an image file?

When using the header function in PHP to send headers, it can affect the output of an image file by changing the content type header. This is important when serving image files from a PHP script, as the correct content type must be set to ensure the image is displayed correctly in the browser. To fix this issue, you can use the header function to set the content type to image/jpeg, image/png, or any other appropriate image format before outputting the image data.

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

// Output the image file
readfile('image.jpg');
?>