Welcome GuestLogin

A Drunken Wiki


A Drunken Wiki is the companion wiki to the blog "A Drunken Madman", written by Jason Brown.


Search the wiki

Vaccination Saves Lives: Stop The Australian Vaccination Network

Find me on Twitter, Facebook, Google+, Pinterest and LinkedIn

Pin It

Alpha Archive Atheism Blue Mountains Climbing Cycling Development DotNet GPS Guitar HMHB JavaScript JQuery LINQ Meta MTB Music Perl Powershell RNP SharePoint Skepticism Social Media Training Ukulele Vaccines Visual Studio WebParts Woodford Festival 2011 XML



Modified on 2012/01/04 13:51 by Administrator Categorized as Development, Perl
This is an archive of an old article from the Perl-Win32 section of InfiniteMonkeys Web programming Resource, first published in about 2001-2002

Simple graphical counters with Image::Magick
by : Atrax

Graphical counters are still amazingly popular on the web, and while most experienced web developers wouldn't be seen dead using one, at least they give us a nice opportunity to show a little more of Image::Magick's capabilities. In this case, annotating text to images on-the-fly.

OK, so you and I don't necessarily like counters, but hey, everyone knows what they are, everyone's seen at least one, and they give us this excellent opportunity to show off some more Image::Magick tricks.

SO, what are we going to do? Well, quite simple really, we're going to make our own counter system. I'll only be showing you the counter code itself, any sign-up or maintainence pages are down to you, but some of the tutorials on this site should help you on the way, should you want to deploy one of these. For our example recipe, we'll be using :

2Kg prime Windows 2000 Server
850g finely sieved SQL Server 2000
One bowl of ActivePerl
2 cups of PerlMagick
A dash of SQL
A small pinch of Win32::OLE
A garnish of GIF89a, or other suitable graphics format to taste

First, wash your hands. Then let's create the base image we'll be making a counter out of. Fire up your editor (Photoshop is nice, but any GIF-capable editor will do) and make one. I made this :

If you think this adds too much of a cheesy flavour, feel free to substitute your own

It's fairly obvious where the numbers will go eventually. I placed this into my lightly greased cgi-bin and started on the meat of the recipe.

I created a SQL Server table, in my 'tests' database, called 'tblPerlCounters'. It has three fields (I could live with two, but I wanted a textual description, an ID field and the count field itself). Add a new record, noting the ID number, set aside and move on.

next, I created counter.pl as a new text file, and placed these declarations at the top:

use Image::Magick;
use Win32::OLE;
use Win32::OLE::Variant qw(:DEFAULT nothing);

You don't need the initial shebang line under Win32, but I like it, and it's a good habit.

Following this, I added a sub to pull the current number of hits from the database

sub getCurrentCount	{
	$id = $ENV{'query_string'};
	if($id eq '')	{
		return 'error : no ID';
	$strConn = <<EOF;
	$strSQL = <<EOF;
	UPDATE tblPerlCounters 
	SET counterCount = counterCount +1 
	WHERE counterID = $id; 
	SELECT counterCount 
	FROM tblPerlCounters 
	WHERE counterID = $id;
	$objConn = Win32::OLE->new('ADODB.Connection');
	$objRS = $objConn->execute($strSQL);
	if($objRS->{EOF} ne 0)	{
		return 'fatal error!';
	}	else	{
		$count = $objRS->fields(0)->{value};
		$objRS = nothing;
		$objConn = nothing;
		return $count;

As you can see, this function opens a SQL Server connection with Win32::OLE and executes against it a SQL statement which first UPDATEs the record, then SELECTs the page count. It's meant to be called with the format '/cgi-bin/counter.pl?', and allows for two error conditions. The first is in case the script is called without an ID number appended, the second is meant to handle invalid IDs. There are a number of other errors I could be trapping, but I'm keeping it simple. It either returns the current count, or a short string with a simple error description. Simple so far? Well, you'll like the next bit, which is preparing the image manipulation code and stringing it all together. Tasty!

First, I create this sub

sub annotateImage	{
	my $im = Image::Magick->new;
	$im->Annotate(font=>'Arial', text=>$_[0|0], x=>20, y=>23, pointsize=>10, fill=>'red');
	print "Content-Type: image/gif\n\n";

Line by line, here we go

Create new Image::Magick object
Read in our template image
Annotate the image in 10pt red Arial, 20px in on the x axis, and 23 points in on the y axis, the text passed to the sub as the first argument
Write out our HTTP header
Stream the annotated image to STDOUT using the '-' character
Really very simple, so the observant among you will note that I can now simple call this sub like so :


Et voila, a tasty dish in a matter of minutes. Of course in cooking, presentation is very important. Just include a little HTML tag in your test page like so (my counter's ID is 1200, yours will rather depend)

And serve. 


ScrewTurn Wiki version Some of the icons created by FamFamFam.