Posts Tagged ‘force download images’

Setting headers for dynamic images with PHP

Written by pelister. Posted in PHP

PHP provides an amazing library called Graphics Draw ( GD ) to generate images. You can create and output the images directly to the browser or you can store it as a image file. For example Google Charts API generates QR Code images and outputs it directly to the browser. You can do the same with PHP GD functions that can generate jpg, gif or png and send them as output, but the default content type of PHP is text or html. So when you output an image to the browser you need to tell the browser, hey look! now I am sending an image, you should not treat it as text or html.

For example when the browser requests a PHP or HTML page, browsers sends some information to the server, the server then looks for the file requested, if it is available it sends back the content along with some information of how to treat the content. That “some information” is called header.

http headers diagram

Techlister© - 2012