How can the Content-Type header be used effectively when displaying images in PHP?
When displaying images in PHP, it is important to set the Content-Type header to inform the browser that the response contains image data. This ensures that the browser interprets the data correctly and displays the image properly. To do this, you can use the header() function in PHP to set the Content-Type header to the appropriate image format, such as image/jpeg or image/png.
<?php
// Set the Content-Type header to display an image
header('Content-Type: image/jpeg');
// Output the image data
$imageData = file_get_contents('image.jpg');
echo $imageData;
?>
Related Questions
- How can the code snippet provided be modified to ensure proper functionality?
- How can PHP developers optimize their code to handle file manipulation tasks more effectively, such as changing file extensions before uploading?
- How can WordPress be configured to redirect links with specific IDs to URLs stored in a database without the need for additional PHP files?