Chapter 5. Fundamentals of dynamic graph generation
Prev | Part II. Basic graph creation | Next

## Chapter 5. Fundamentals of dynamic graph generation

What you will learn in this chapter. The purpose of this chapter is to put dynamic image generation in perspective and illustrate how HTML tags is used to call image generating scripts. Even if You are familiar with PHP it is strongly recommended to quickly browse through this chapter to make sure all concepts are known. If You fully understand and can explain the concept of MIME types, HTTP streams and why the "Headers already sent error" error is a very common error when generating dynamic images with PHP it is probably safe to skip this chapter. Otherwise it might be wise to read through this chapter at least once.

## 5.1. Making sense of HTTP streams and MIME types

The following explanation is slightly simplified since a full description of the HTTP protocol would bring us a bit too far in this manual

1. A client (e.g. browser) requests data from the server by issuing a GET (or possible a POST) command to the server. This is what happens when you enter a URI in the address bar in the browser.

2. The server replies with a data stream (or an error if the requested data wasn't available). This data stream is prepended with header (MIME header) that tells the client (e.g. the browser) how to interpret the data that follows. The most common type (and the default type if no header is sent by a faulty server) is "text/html" . This tells the client to interpret the data as plain text with embedded HTML encoding.

   When the data is to be interpreted as an image the header will instead be one of the image headers, for example "image/png" or "image/jpeg". When the client receives this header it will interpret all the following data as an image encoded in the indicated format.

   The important thing to keep in mind here is that each server reply (initiated by a call from the client) can only have one and only one MIME type. This is the key to further understanding the specific issues with dynamic image generation. This explains why if a PHP script running on the server sends a header first indicating that the following data it sends should be interpreted by the client as an image it cannot also include text since only one header can be used for one HTTP stream.

What happens on a WEB-page with, for example, multiple <img> tags is that the browser issues a separate GET command for each of these images. So even though it can look like a fetching a single WEB-page can have different content each individual request to the server only have one single MIME type.

Prev | Up | Next
4.2. Graphing the number of sun spots during the 19th Century | Home | 5.2. What is an image?