Outputting an Image in PHP
Outputting an image to a browser in PHP is a straightforward task. Here's how you can do it:
Set the Content-Type Header:
Specify the MIME type of the image using the header() function. For example:
header('Content-Type: image/jpeg');
Set the Content-Length Header:
Indicate the size of the image file in bytes using the filesize() function:
header('Content-Length: ' . filesize($file));
Output the Image File:
Use the readfile() function to read and output the contents of the image file:
readfile($file);
Here's an example code snippet that puts it all together:
$file = '../image.jpg'; $type = 'image/jpeg'; header('Content-Type:'.$type); header('Content-Length: ' . filesize($file)); readfile($file);
By following these steps, you can output an image to the browser, allowing the user to view or download it.
The above is the detailed content of How Can I Output an Image in PHP?. For more information, please follow other related articles on the PHP Chinese website!