Shaun Mccran

My digital playground

28
J
U
N
2010

My handy custom font header building function

I've been putting together a personal site for a little while now, and one of the more common pieces of work I've had to do is creating the page headers from a custom font library in Photoshop. The site is 'comic' themed, and as such has an appropriate font (not comic sans!) .

Rather than having to create a new image for each page, I thought I'd cut up the alphabet, and create a function to turn a string into a set of graphics.

Initially this is more work than creating each image, but once you've cut up each letter of the alphabet your job is done. The function simply accepts a string, and uses the Java split() method to create an Array of each letter in the string. It then loops over the string, and creates an image html tag for each item.

view plain print about
1<cffunction name="createFont" access="public" output="true" returntype="void" hint="returns a graphic version of a string">
2
3<cfargument name="string" type="string" required="true" hint="string to change into a font">
4
5<cfset var stringIndex = "">
6<cfset var showCaps = 1>
7<cfset var tokens = arguments.string.split("")>
8<cfset var token = "">
9<cfset var rootpath = pathToYourImages>
10
11<cfloop array="#tokens#" index="token">
12
13<cfif len(token)>
14
15<cfif token EQ " ">
16&nbsp;<cfset showCaps = 1>
17
18<cfelse>
19
20<img src="#rootPath#view/images/letters/<cfif showCaps>caps<cfelse>lower</cfif>/#token#.jpg">
21
22<cfset showCaps = 0>
23
24</cfif>
25
26</cfif>
27
28</cfloop>
29
30</cffunction>

A straight forward, simple bit of code. My only issue arose when I tried to roll it out onto my live Coldfusion server, which is running Coldfusion server 7. It turns out that looping over Arrays like this was introduced in Coldfusion 8! So I'll have to code a work around.

Backwards compatible version:

view plain print about
1<cfloop from="1" to="#Arraylen(tokens)#" index="token">
2
3<cfif len(tokens[token])>
4
5<cfif tokens[token] EQ " ">
6&nbsp;<cfset showCaps = 1>
7
8<cfelse>
9
10<img src="#rootPath#view/images/letters/<cfif showCaps>caps<cfelse>lower</cfif>/#tokens[token]#.jpg">
11
12<cfset showCaps = 0>
13
14</cfif>
15
16</cfif>
17
18</cfloop>

I have since discovered that you can wrap the whole thing in a h1, h2 or h3 tag and search engines will read the images as if they are text, so your SEO is not negatively affected.

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top