ColdFusion 8 is bringing the web counter back!

Justin Timberlake may be a little more popular than ColdFusion for bringing sexy back, but let me share how ColdFusion 8 is bringing the web counter back. What’s the web counter?

Back in the “old” days, your web site wasn’t truly a web site until it had a web counter. This little graphical doodad would show you how many visitors had hit your web site. Here is an example:

You would paste once of these bad boys on your site and reload your page watching the numbers go up. And if the number actually went up twice in one hit - holy smokes - that means someone else actually hit your web page!

Various services were set up to serve up these counters, and some, like Site Meter are still around.

For fun, I decided to try to build one of these myself. I began by creating a simple CFC to handle storing and retrieving counter information. I didn’t want to bother with a database and instead just relied on the Server scope. Yes, this means the stats won’t really count, but this is all just for fun anyway. Here is what I started off with:

<cffunction name="init" access="public" returnType="counter" output="false"> <cfset initData()> <cfreturn this> </cffunction>

<cffunction name=”initData” access=”public” returnType=”void” output=”false”> <!— setup initial data store. —> <cfif not structKeyExists(server, “counter”)> <cfset server.counter = structNew()> </cfif> </cffunction>

<cffunction name=”getCount” access=”public” returnType=”numeric” output=”false”> <cfargument name=”client” type=”string” required=”true”>

&lt;cfif not structKeyExists(server.counter, arguments.client)&gt;
	&lt;cfset server.counter[arguments.client] = 0&gt;
&lt;/cfif&gt;

&lt;cfreturn ++server.counter[arguments.client]&gt; &lt;/cffunction&gt; </code>

I should have a few CFLOCKs in there, but again, I wanted to keep it simple. The code will accept any “client” variable which represents the counter. When requested, I use ++X form to both increase the value and return the increased value. (Boy do I love the new syntax features in ColdFusion 8!) I tested this code by itself before adding the image stuff to ensure everything worked fine.

Next I created a method that would generate an image. “Real” hit counters give you multiple options for sizes, counter styles, etc., but I kept mine simple. You can provide an optional background and text color only. I hard coded sizes that I thought made sense. The only complex part was handling the positioning of the numbers. I blogged about how to handle this a few weeks ago. I haven’t yet had a chance to add this to ImageUtils (the project Ben and I started to wrap up common image tasks) but will get to that soon. Here is the code for the function to return the counter image:

<cffunction name="makeCounter" access="public" returnType="any" output="false"> <cfargument name="client" type="string" required="true"> <cfargument name="bgcolor" type="string" required="false" default="black"> <cfargument name="textcolor" type="string" required="false" default="white">

&lt;!--- get the number ---&gt;
&lt;cfset var count = getCount(arguments.client)&gt;
&lt;!--- make the canvas ---&gt;
&lt;cfset var img = imageNew("", 150, 40, "rgb", arguments.bgcolor)&gt;
&lt;!--- set the text props ---&gt;
&lt;cfset var tProps = { style='bold', size=24, font='Arial-BoldMT' }&gt;

&lt;cfset var buffered = imageGetBufferedImage(img)&gt;
&lt;cfset var context = buffered.getGraphics().getFontRenderContext()&gt;
&lt;cfset var font = createObject("java", "java.awt.Font")&gt;
&lt;cfset var textFont = Font.init(tProps.font, Font.BOLD, javacast("int", tProps.size))&gt;
&lt;cfset var textLayout = createObject("java", "java.awt.font.TextLayout").init(javaCast("string",count), textFont, context)&gt;
&lt;cfset var textBounds = textLayout.getBounds()&gt;
&lt;cfset var twidth = textBounds.getWidth()&gt;
&lt;cfset var theight = textBounds.getHeight()&gt;
&lt;cfset var newx = ""&gt;
&lt;cfset var newy = ""&gt;
		
&lt;!--- text color ---&gt;
&lt;cfset imageSetDrawingColor(img, arguments.textColor)&gt;

&lt;!--- handle centering crap ---&gt;	
&lt;!---
when drawing text, you specify X, Y as the bottom left corner.
So we need to position ourselves at Total Height / 2 + Height of Text / 2
---&gt;
&lt;cfset newx = (img.width/2 - tWidth/2)&gt;
&lt;cfset newy = (img.height/2 + tHeight/2)&gt;

&lt;cfset imageDrawText(img, count, newx, newy, tProps)&gt;

&lt;cfreturn img&gt; &lt;/cffunction&gt; </code>

As you can see, more than 50% of the code is just for positioning the darn text, so if I did have that nice UDF handy, the function would have been a heck of a lot slimmer.

All that’s left now is a file to spit out the image. Let’s look at how I did that:

<cfparam name="url.client" default=""> <cfparam name="url.bgcolor" default=""> <cfparam name="url.textcolor" default="">

<cfif not len(url.client)> <cfthrow message=”You must supply a Client variable in the query string.”> </cfif>

<cfinvoke component=”#application.counter#” method=”makeCounter” returnVariable=”img”> <cfinvokeargument name=”client” value=”#url.client#”> <cfif len(url.bgcolor)> <cfinvokeargument name=”bgcolor” value=”#url.bgcolor#”> </cfif> <cfif len(url.textcolor)> <cfinvokeargument name=”textcolor” value=”#url.textcolor#”> </cfif> </cfinvoke>

<cfset bos = CreateObject(“java”,”java.io.ByteArrayOutputStream”).init()/> <cfset imageio = CreateObject(“java”,”javax.imageio.ImageIO”).write(imagegetbufferedimage(img), “PNG”, bos)/> <cfset bos.close()/>

<cfcontent type=”images/gif” variable=”#bos.toByteArray()#” reset=”true”> </code>

The code begins with a few cfparams and validation on the client variable. I then call my component and pass in the relevant values. So far so good. Now what the heck is the Java?

ColdFusion 8 has a bug with images created using imageNew that are not based on a ‘real’ image. If you try to get the binary data from the image, or try to pass it to cfcontent, you will get an image.

Jason Delmore of Adobe came up with this solution. It provides an alternate way to get binary data out and then supply it to the cfcontent tag. I’ll be adding this to ImageUtils too!

So now that I’ve gone through all of that, here is a simple counter for the client parishilton:

Sorry, old demos are removed.

The URL I used was http://www.coldfusionjedi.com/demos/counterservice/image.cfm?client=parishilton.

Now let’s make it a bit fancier and supply some colors: <!–

That URL was http://www.coldfusionjedi.com/demos/counterservice/image.cfm?client=parishilton&bgcolor=pink&textcolor=green. –> Sorry, old demos are removed.

Note - I ran into a font issue when moving my sample code from my Mac to my Windows machine. The code on the live server is using “Arial Black” for the font instead of the one specified in the code above.

So a total waste of time - but a fun way to spend lunch. Enjoy!

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments