Setting headers for dynamic images with PHP

Setting headers for dynamic images with PHP
by Janeth Kent Date: 30-04-2013 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.

Header information is sent using header() in PHP by passing the header string as parameter. There are lots of header strings for various operations and writing a complete list will be beyond the scope of this article. But let us look into setting up headers for image output. You can clearly see the headers received by the browser when a page is loaded. You can see this in real-time by installing Firebug.

For example take a look at the following 2 screenshots.


Headers for HTML Content received by the browser

Headers for images received by the browser


You could see Content-Type changes for html and images sent to the browser, but these headers must be sent to the browser before you send any other output.


This is the usual panic attack faced by PHP programmers, but let me tell you don’t panic. The problem might have occurred for the following reasons.

Intentional mistakes

  • You might have used echo, print or other functions like var_dump() that produces output before sending the header.
  • <html> tags before <?php – placing an <html> tag before sending header will send the output to the browser.

Yeah, I can hear you saying – “I am not that stupid”. Well, you are not, but the error might have occurred for the following reasons.

Unintentional mistakes

  • Blank white-spaces before <?php – a single white space is enough to give you a mild heart attack.
  • UTF-8 Byte Order Mark( BOM ) – I would like to call it BOM(B) ready to explode as soon as you execute your PHP script. The byte order mark (BOM) is a Unicode character used to signal the byte order of a text file or stream.
  • error messages, warnings or notices that occurred somewhere before sending the header.
    $im = imagecreatetruecolor( 200, 200 );
    $color = imagecolorallocate( $im, 255, 0, 0 );
    imageellipse( $im, 100, 100, 150, 150, $color );
    header( "Content-type: image/png" );
    imagepng( $im );

The above code generates an image, and in the final line imagepng() function sends the image directly to the browser, so we need to tell the browser that we are sending an image and so theheader() function specifying the content type is set. In the above code we don’t have any  output functions before the header is sent, no blank space before PHP tag, but still you will get the error “Headers already sent”. Most of the time this error occurs because of the BOM character that is embedded within the document while saving them. You could remove them easily. Follow these simple steps.

  • Install Notepad++
  • From the – Encoding menu at the top, select “Convert to UTF-8 without BOM”, then save it.


You will get rid of that BOM character and your PHP will run fine. Now the sample code will show an image in the browser, there arises another problem – You try to save that image, and you will see that while saving images generated by PHP your image filename will be the same as the PHP script that sent the image to the browser.


You can see .php is the extension instead of png, jpg or gif. Now if you want to give a proper name and type for the image output that is sent to the browser you have to send another header like the one below.

header(&quot;Content-Disposition: inline; filename=myimage.png&quot;);

The above line says the content sent to the browser is to be opened inline and to be called asmyimage.png. But there is also another type of Content-Disposition, that is attachment.The difference between inline and attachment is

  • inline – tells the browser to display it inline, the browser will attempt to display the content, for example png, jpg, gif, pdf files etc. But if the browser could not to display the content it will show the download dialog to save it in some other location.
  • attachment – will force the browser to open the download dialog, so the browser will not attempt to display the content even if it can, but shows the download dialog.



by Janeth Kent Date: 30-04-2013 php hits : 3849  
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.


Related Posts

Examine the 10 key PHP functions I use frequently

PHP never ceases to surprise me with its built-in capabilities. These are a few of the functions I find most fascinating.   1. Levenshtein This function uses the Levenshtein algorithm to calculate the…

How to Track Flight Status in real-time using the Flight Tracker API

The Flight Tracker API provides developers with the ability to access real-time flight status, which is extremely useful for integrating historical tracking or live queries of air traffic into your…

What is a JWT token and how does it work?

JWT tokens are a standard used to create application access tokens, enabling user authentication in web applications. Specifically, it follows the RFC 7519 standard. What is a JWT token A JWT token…

PHP - The Singleton Pattern

The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a method for limiting the number of instances of an object to just one.…

How to Send Email from an HTML Contact Form

In today’s article we will write about how to make a working form that upon hitting that submit button will be functional and send the email (to you as a…

The State of PHP 8: new features and changes

PHP 8.0 has been released last November 26: let's discover together the main innovations that the new version introduces in this language. PHP is one of the most popular programming languages…

HTTP Cookies: how they work and how to use them

Today we are going to write about the way to store data in a browser, why websites use cookies and how they work in detail. Continue reading to find out how…

The most popular Array Sorting Algorithms In PHP

There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations,…

MySQL 8.0 is now fully supported in PHP 7.4

MySQL and PHP is a love story that started long time ago. However the love story with MySQL 8.0 was a bit slower to start… but don’t worry it rules…

A roadmap to becoming a web developer in 2019

There are plenty of tutorials online, which won't cost you a cent. If you are sufficiently self-driven and interested, you have no difficulty training yourself. The point to learn coding…

10 PHP code snippets to work with dates

Here we have some set of Useful PHP Snippets, which are useful for PHP Developers. In this tutorial we'll show you the 10 PHP date snippets you can use on…

8 Free PHP Books to Read in Summer 2018

In this article, we've listed 8 free PHP books that can help you to learn new approaches to solving problems and keep your skill up to date.   Practical PHP Testing This book…