Now you have a blank white image that is 300×300 pixels in size. Lets do something with it. Lets tell the world how much we love GDLib. Enter this under what you already have:
<?php
imagestring ($im, 5, 0, 0, "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:
<?php
imagerectangle ($im,50,50,100,100,$blue);
?>
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:
<?php
header ("Content-type: image/png");
$im = imagecreate (300, 300);
$white = imagecolorallocate ($im,255,255,255);
$blue = imagecolorallocate ($im,0,0,255);
$black = imagecolorallocate ($im,0,0,0);
imagestring ($im, 5, 0, 0, "I Love GDLib!!!", $blue);
imagefilledrectangle ($im,50,50,100,100,$blue);
imagerectangle ($im,50,50,100,100,$black);
imagepng($im);
?>
Now that you know the basics of image dynamics, we’ll move on to a real world application: dynamic graphs.