PHPBuilder - Dynamic Graphs with PHP, MySQL and GD Page 3

RSS Twitter

Dynamic Graphs with PHP, MySQL and GD - Page 3

by: Ramsey Nasser
February 19, 2003

Now you have a blank white image that is 300x300 pixels in size. Lets do something with it. Lets tell the world how much we love GDLib. Enter this under what you already have:

($im500"I Love GDLib!!!"$blue);
Now take a look at your image. If all went well, you should have the text "I Love GDLib!!!" written in blue in your previously empty image. Lets examine why this happened. The first argument, $im, refers to which image the text should be written to. The second refers to which of the five built in fonts to use. Each is mono space, with the width of the characters matching the font number (ie. Font 3 will have a character width of 3 pixels). The third and fourth arguments are the x and y coordinates of the upper left hand corner of the string (a string put at 0,0 will show, but a string put at 0, 300 would go completely off the image). The fifth argument is the actual string, and the last is the color. The color must be one of the colors allocated from before. Now for some geometry. Were going to draw a rectangle using the same blue color as the text. The function to draw rectangles is imagerectangle (complex, eh?). Enter this line into your script and we'll analyze:

Take a look at the image, and, if all went well, you'll see the outline of a rectangle drawn in blue. The first argument, $im, refers to which image the rectangle is to be drawn on (like the imageallocate and the imagestring function). The last argument specifies which color it is to be drawn in. Only an allocated color can be used. The 2nd to 5th arguments are the x1,y1, x2 and y2 coordinates of the rectangle. x1,y1 is the upper left hand corner, and x2, y2 is the lower right hand corner. As you can see, most image functions have similar arguments. They all must identify which image they are to be drawn to as their first argument, and most require the color they should be in as their last argument. Imagerectangle and image string are two of the many drawing functions that come with GDLib. Others include imagedrawcircle, imagedrawpolygon, imageline, and many, many others. This article will only scratch the surface of this powerful extension, and the only way to learn how to use all the drawing functions is to open up the PHP manual and experiment. Now try the same coding again, only this time write imagefilledrectangle instead of imagerectangle. If done properly, you should have, as the name implies, a filled rectangle in blue. Now allocate the color black to a variable, and use it in an imagerectangle after the imagefilledrectangle to get a nice outline. To finish it all off, add imagepng($im); to draw the image. The final code is below:

("Content-type: image/png");
$im imagecreate (300300);
$white imagecolorallocate ($im,255,255,255);
$blue imagecolorallocate ($im,0,0,255);
$black imagecolorallocate ($im,0,0,0);
imagestring ($im500"I Love GDLib!!!"$blue);
imagefilledrectangle ($im,50,50,100,100,$blue);
imagerectangle ($im,50,50,100,100,$black);
Now that you know the basics of image dynamics, we'll move on to a real world application: dynamic graphs.

« Previous Page
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Ramsey Nasser



(Maximum characters: 1200). You have characters left.