What is the purpose of using the header function with 'Content-type: image/jpeg' in PHP?

When using the header function with 'Content-type: image/jpeg' in PHP, the purpose is to specify the type of content being sent to the browser as an image file in JPEG format. This is important for ensuring that the browser interprets the data correctly and displays the image as intended. Without setting the correct content type, the browser may not render the image properly or may try to display it as plain text.

<?php
// Set the content type header to specify that the data being sent is an image in JPEG format
header('Content-type: image/jpeg');

// Output the image data
echo file_get_contents('image.jpg');
?>