Outputting Images in PHP
When working with images in PHP, you may encounter situations where you need to display them on a web page. In this scenario, understanding how to output images to the browser is crucial.
Consider the following code snippet:
<p>I have an image $file ( eg ../image.jpg )</p> <p>which has a mime type $type</p> <p>How can I output it to the browser?</p>
To display the image effectively, we need to provide the browser with essential information. Here's how it's done:
<?php $file = '../image.jpg'; $type = 'image/jpeg'; header('Content-Type:'.$type); header('Content-Length: ' . filesize($file)); readfile($file); ?>
Breaking down the code:
By executing this code, the PHP script will correctly display the image in the browser. Note that the provided file path ($file) and MIME type ($type) should be adjusted accordingly based on your specific image and its location.
The above is the detailed content of How to Output Images to the Browser Using PHP?. For more information, please follow other related articles on the PHP Chinese website!