Domain Empire

Tutorial: A Basic intro to using PHP GD functions to generate and manipulate images.

Spaceship Spaceship
Watch
Impact
6
Installing the GD library on Windows:

Browse to your PHP directory, most likely c:php and search for the the file php_gd2.dll. It's probably in your c:phpextensions directory. Copy it to to your PHP directory, probably c:php. Now open up your php.ini file which is probably located at c:windowsphp.ini and search for ;extension=php_gd2.dll and uncomment it by removing the semi-colon.

Creating an image with PHP
PHP:
<html>
<body>
<?php

    //Creating the Image
    $im = imagecreate(320, 200);

    //Defining colors
    $black = imagecolorallocate($im, 0, 0, 0);
    $white = imagecolorallocate($im, 255, 255, 255);
    $wdtcolor = imagecolorallocate($im, 24, 40, 80);

    //Drawing white background and black outline
    imagefilledrectangle($im, 0, 0, 319, 199, $white);
    imagerectangle($im, 0, 0, 319, 199, $black);

    //Drawing text on the image
    imagestring($im, 5, 110, 100, "WebDev", $wdtcolor);

    //Creating the image output in png format
    imagepng($im,'image.png');

    //freeing up any memory associated with the image
    imagedestroy($im);
?>

<img src="image.png" width="320" height="200">
</body>
</html>

imagecreate()
We use the imagecreate function to create a palette based image. The imagecreate function takes in two parameters, width and height in terms of pixels. We assign $im as our image identifier. The image identifier is used to distinguish one image from another and is required for all the image editing functions.

imagecolorallocate()
We use the imagecolorallocate function to define the colors that we're going to use in the image. Imagecolorallocate takes in four paramaters, the image identifier, and the Red, Green, and Blue (RGB) components of a color. If you have two images you must assign the colors separately for each image. So if you were going to use the color blue in an image you would have to declare it twice, once with each image identifier. Here's a few more examples for different colors:
PHP:
$red = imagecolorallocate($im, 255, 0, 0);
$green = imagecolorallocate($im, 0, 255, 0);
$blue = imagecolorallocate($im, 0, 0, 255);

imagefilledrectangle()
We use the imagefilledrectangle function to fill in our image with a white back ground. The imagefilledrectangle function takes in six parameters. The first paramater is the image identifier, the next four are the coordinates for the upper left and lower right corner of the rectangle. The last parameter is the color identifier which was created using the imagecollorallocate function.

imagerectangle()
The image rectangle function is identical to the imagefilledrectangle function except for it only draws an outline.

imagestring()
We use the image string function to print out a string on our image. The imagestring function takes in six paramaters. The first parameter as usual is the image identifier. The next parameter is the font size which by default only has five different sizes. Checkout the Imagettftext and imageloadfont functions for more info on using different fonts. The next two paramaters are the horizontal and vertical coordinates of the upper left corner of the text and the last parameter is the color identifier.

imagedestory()
The image destory function simply frees up any memory associated with the image.

The PHP GD functions can be useful for displaying poll results, creating charts, graphs and more.

A simple example of a vertical bar graph
PHP:
<html>
<body>
<?php

    //Creating the Image
    $im = imagecreate(320, 200);

    //Defining colors
    $black = imagecolorallocate($im, 0, 0, 0);
    $white = imagecolorallocate($im, 255, 255, 255);
    $blue = imagecolorallocate($im, 24, 40, 80);

    //Drawing white background and black outline
    imagefilledrectangle($im, 0, 0, 319, 199, $white);
    imagerectangle($im, 0, 0, 319, 199, $black);


    //Drawing Vertical Bars
    imagefilledrectangle($im, 2, 20, 40, 160, $blue);
    imagefilledrectangle($im, 42, 60, 80, 160, $blue);
    imagefilledrectangle($im, 82, 100, 120, 160, $blue);
    imagefilledrectangle($im, 122, 80, 160, 160, $blue);
    imagefilledrectangle($im, 162, 10, 200, 160, $blue);
    imagefilledrectangle($im, 202, 40, 240, 160, $blue);
    imagefilledrectangle($im, 242, 75, 280, 160, $blue);
    imagefilledrectangle($im, 282, 50, 320, 160, $blue);

    //Printing out vertical Strings beneath each bar
    imagestringup($im, 5, 15, 195, "Op1", $black);
    imagestringup($im, 5, 55, 195, "Op2", $black);
    imagestringup($im, 5, 95, 195, "Op3", $black);
    imagestringup($im, 5, 135, 195, "Op4", $black);
    imagestringup($im, 5, 175, 195, "Op5", $black);
    imagestringup($im, 5, 215, 195, "Op6", $black);
    imagestringup($im, 5, 255, 195, "Op7", $black);
    imagestringup($im, 5, 295, 195, "Op8", $black);


    //Creating the image output in png format
    imagepng($im,'image.png');

    //freeing up any memory associated with the image
    imagedestroy($im);
?>

<img src="image.png" width="320" height="200">
</body>
</html>

You could build on this and dynamically resize the bars according to results you pull from a database or text file, or other calculations you may use in your scripts.

imagestringup()
In the above example we used another function imagestringup. The imagestringup function works just like the imagestring function except for it prints vertically from bottom to top and the coordinates start at the lower left corner of the text.

This is just the very basics of using the PHP GD functions to hopefully help get you an idea of how they work and get you started using them. But there's so much more you can do with PHP and GD. Take a look here http://php.net/image for a complete list of functions and a whole lot more info.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
u r a genius!!!!!!!!!!!!!!!


thanks!!

been searching for time something like this
 
0
•••
Hey, cool! Nice to see someone found it useful. :) The gd library can be quite useful and even fun once you mess around with it a bit.
 
0
•••
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back