Making usage statistics on a web site is one of the most enjoyable things for me on the Internet. Thanks to the technology, you can see each click of each visitor, the date of the visiting, and how many seconds the user was reading your site. I use this feature to track which of my articles was read for the longest time. In this article I will show you how to store the statistics in a MySQL database, show the web stats in an HTML table and make figures based on the stats using the GD library.
Making Usage Statistics in PHP - Creating the Image Library (Page 5 of 7 )
Having the data, let's start to deal with the image creation library of PHP (GD). Its output is GIF/PNG/JPEG. It uses image resources. These are PHP variables representing images. Usually, in a program there is only one image resource. You can create an image resource using one of the following functions:
ImageCreate (int width, int height) - Creates a blank image resource
ImageCreateTrueColor (int width, int height) - Creates a blank true color image resource
ImageCreateFromGIF (string filename) - Creates an image resource using a given picture file
ImageCreateFromJPEG (string filename) - Creates an image resource using a given picture file
ImageCreateFromPNG (string filename) - Creates an image resource using a given picture file
This code can be good for this, but don't forget to call the header function because it's not HTML output!
Then you can start to draw the picture. Here are some of the functions - read them as we will use some to make the Top 10 stats. For more, see the PHP manual.
ImageColorAllocate (resource image, int red, int green, int blue) - Prepares an RGB color to use
ImageLine (resource image, int x1, int y1, int x2, int y2, int color) - Draws a line
ImageRectangle (resource image, int x1, int y1, int x2, int y2, int col) - Draws a rectangle
ImagePolygon (resource image, array points, int num_points, int color) - Draws a polygon using an array
ImageFilledRectangle (resource image, int x1, int y1, int x2, int y2, int color) - Draws a filled rectangle
ImageTTFText (resource image, int size, int angle, int x, int y, int color, string fontfile, string text) - Draws a TrueType text
Below you can read the code. It can be a little confusing, especially the numeric expressions, because you have to think in X and Y coordinates. But try it, you'll get the hang of it!