A text access counter
The Increment subroutine
The complete text access counter script
Seeding the Counter
Adding the Counter to Your HTML Page
With Server Side Includes
Without Server Side Includes
Creating the Template File
Modifying the access.pl Script
A graphical access counter
Incrementing the Counter
Creating the GIF Image
Returning the Graphical Counter Image
The Graphical Counter Script
Calling the Counter with the <IMG> Tag
Returning the Graphical Counter Image
Now that the counter value is incremented and saved to the file and the graphical counter image is created, you are ready to return the graphical counter image from your CGI script. Just before returning the image, you can call the gd library function
gdImageInterlace(im_final, 1);
which interlaces the im_final image. Interlacing displays the GIF image incrementally as it is downloaded in certain Web browsers that support interlaced GIFs. An interlaced GIF first appears somewhat distorted and then gradually become more clear as the remainder of the file is downloaded across the Internet. When you use interlaced GIF images, the users viewing your Web pages see an image more quickly, even if it's a little distorted. This makes it appear as though the whole image is downloading more quickly. Because the graphical counter image is not very large, interlacing is not imperative. However, if you used one of the larger digit styles available from the Digit Mania Web page, you would probably want to interlace your image.
Before outputting the graphical counter image, you must return a parsed header, as you did for the text access counter. However, the parsed header for the graphical image counter needs to specify that the data being returned is a GIF image. The following lines output the parsed header and the graphical counter image.
printf("Content-type: image/gif\n\n");
gdImageGif(im_final, stdout);
With the graphical counter image returned for inclusion in your Web page, you can now destroy the im_final image with the statement
gdImageDestroy(im_final);
You should always call the gdImageDestroy function when you are finished with a gd Image because it frees up any memory that was allocated for that image.