Code Area PHP Tips and Tricks

Return Image as response by PHP

Return Image as response By PHP

To return an image as a request response by PHP you fpassthru function of PHP. I have included a sample code snippet, which will help you to understand how to do this.

From the above code snippet, you can see I have opened an image with binary mode by fopen($image, rb). And then send that with appropriate headers by header(“Content-Type: image/png”). After that dumping that image by using fpassthru, and finally stop the execution of PHP. It’s really easy to return image as response by PHP