Login | Register
Nerd ParadiseArtisanal tutorials since 1999
Sometimes using image files for images on your website doesn't always cut it. If you want to create a real-time graph or other on-the-fly images through PHP, then you need to pull out the big guns.


First, you can start with a template from an already existing file. This function will create an PHP image object reference from a PNG file. This image object reference will be used to pass into the other functions outlined below. Similarly, there's also imagecreatefromjpeg and imagecreatefromgif.

imagecreatetruecolor(width, height)

If you don't need to start with a template and just want a blank image of a given size, then you'll want to use this. This creates a black image of the given width and height. This, of course, also returns an image object reference.

imagesetpixel(image, x, y, color)

This will set the pixel at the x-y coordinate of the given image object to a color that you can specify. X and Y are 0-indexed (meaning that 0,0 is the top left corner, not 1,1). Color references come from the next function...

imagecolorallocate(image, red, green, blue)

This function will return a color reference for use in the given picture. I don't know why the image reference is necessary to create a color. This is contrary to pretty much every other programming language I've seen. But PHP is pretty screwy anyway.


This will regurgitate the binary data out to the file being viewed in the browser. This is the equivalent of echo being used for text. This is the echo of images. There are also imagejpeg and imagegif.

header("Content-type: image/png")

Of course if you view the .php file in your browser, it'll appear as text gibberish since the server will assume you are using PHP to generate HTML like you normally do. Before you call imagepng (or imagejpeg or imagegif), you must specify that the MIME type is an image of whatever sort.

So in summary, here is a quick example that creates a color spectrum of any size that you pass to this php file as a width and height parameter in the URL. (like foo.php?width=150&height=40)

    $width = intval($_GET['width']);
    $height = intval($_GET['height']);
    $image = imagecreatetruecolor($width, $height); 
    for ($x = 0; $x < $width; ++$x)
        for ($y = 0; $y < $height; ++$y)
            $r = intval(255 * $x / $width);
            $g = intval(255 * $y / $height);
            $b = 255 - intval(255 * ($x + $y) / ($width + $height));
            $color = imagecolorallocate($image, $r, $g, $b);
            imagesetpixel($image, $x, $y, $color);
    header("Content-type: image/png");

Here are two additional functions that you may find useful...

imagearc(image, center-x, center-y, width, height, angle-begin, angle-end, color)

Most generally, this will draw a single-pixel wide arc. Therefore this can be used to create ellipses and circles. The angle-begin and angle-end parameters are the begin and end points of an incomplete arc in terms of degrees. If you want a closed ellipse or circle, use 0 and 360 for these values respectively.

imagepolygon(image, point list, number of points, color)

This will draw a closed polygon with the given list of points with a 1-pixel-wide line. The list of points is just an array of numbers with x and y coordinates alternating as in array(x1, y1, x2, y2, x3, y3, ...). The number of points seems like a silly redundant parameter since that can be determined from the length of the point list. But then again, this is PHP. And PHP is a silly language. If you want to create a filled polygon, then there is function that works in an identical manner called imagefilledpolygon.

imagegetcolorat(image, x, y)

This will either return the color as an integer for images that don't use an indexed palette or the index of the color in the palette. To extract the RGB values of the color (for non palette indexed images), do the following:

$color = imagegetcolorat($img_instance, 42, 314);
$r = $color & 255;
$color = $color >> 8;
$g = $color & 255;
$color = $color >> 8;
$b = $color & 255;
$color = $color >> 8;
$transparency = $color & 255;

It is important to note that the transparency value is transparency, NOT opacity and is a number between 0 and 127. Opacity means 0 is entirely transparent and 255 is entirely opaque. Transparency means 0 is entirely opaque and 127 is entirely transparent, the opposite of most languages. The reason why PHP does things this way is because PHP is the incarnation of pure evil.
For images with indexed palettes, pass in the returned index value along with the image into a function called imagecolorsforindex($img, $color_index) which will return an array of the RGBT values.