Output an Image in PHP

前端 未结 12 1217
萌比男神i
萌比男神i 2020-11-22 14:33

I have an image $file ( eg ../image.jpg )

which has a mime type $type

How can I output it to the browser?

12条回答
  •  渐次进展
    2020-11-22 14:46

    You can use header to send the right Content-type :

    header('Content-Type: ' . $type);
    

    And readfile to output the content of the image :

    readfile($file);
    


    And maybe (probably not necessary, but, just in case) you'll have to send the Content-Length header too :

    header('Content-Length: ' . filesize($file));
    


    Note : make sure you don't output anything else than your image data (no white space, for instance), or it will no longer be a valid image.

提交回复
热议问题