This might be strange question and you might already now the answer but having a true understanding will help. An image is simply a data stream of octets (bytes) specifying the exact color of a number of pixels. A typical image uses 32 bits to specify the color which means it can encode 2^32 different colors (Homework: How many gray levels is possible with 32 bits?). It would be perfectly possible to send this data stream as it is and indeed this is how the bitmap (BMP) format specifies the data. However, image data usually have a lot of redundancies that can be used to reduce the size of the data that needs to be transmitted to specify any given image. These compression formats are what is more commonly known as for example PNG, JPEG or GIF type of images. So it is actually more correct, in our view, to look upon these formats as different compression techniques rather than as true image formats.
Contrary to popular belief it is not the suffix on a file per se that identifies a particular file as an image on a HTTP server but rather the mapping on the server that instructs the server to send back a particular header for a particular file type (as identified by the file suffix), the MIME type. This might not seem like an important distinction to make but keeping this in mind will help understanding how images are created with a PHP script.
A normal PHP scripts sends back character data (or more likely HTML encoded character data) that is displayed by the browser. However it is nothing that stops the PHP script from sending back data that instead should be interpreted as an image assuming of course we know how to create such data..
How the client (i.e. the browser) should interpret the data sent is all controlled by the header that is sent before the data. So this now leads us to the idea that if our PHP script by some means constructs a valid image, compressed in a known format (say PNG), we can tell the client to interpret the data we send back as an image by just making sure we first send a header indicating that the data is an image.
This could now lead us to the perhaps surprising but crucial insight that if we
call our script that sends back valid image data "myimage.php
"
and open it in a browser, for example by opening the local URI
"http://localhost/myimage.php
" for the browser this is no
different then opening an image directly (for example
"http://localhost/myimage.png
")
The only difference is that in the first case (our PHP script) we create the header and data our self and in the second case the HTTP server looks up the file suffix (*.png) to find out what kind of header to send before reading the file and just passing on the data to the client.
Armed with this new knowledge we realize that our PHP script is an image for all practical purposes and intent from a clients point of view. So we could safely name our PHP script as a target for an <img> tag. This gives us the final and most common way to call a graph script in a HTML page.
<img src="myimage.php">
in exactly the same way as we would do with the old type of images. The "*.php" suffix is no problem since the client (e.g. the browser) don't care what the file ending is. It only cares about what header the server returns. If that header tells the browser that the data received should be interpreted as a PNG image it will do so regardless of the name of the source file on the server.