Generating Mock Images in ColdFusion

Dummy Image!

Dummy Image!

My co-worker Brian passed an interesting lifehacker post along about dummyimage.com.

It’s a cool site that lets you pass in a width and a height to generate an image which could come in really handy for quickly mocking up web pages.

I can’t believe I never thought of doing something like this!

ColdFusion is a great language for doing this sort of thing, so it was trivial to whip something up quickly.

I was originally writing a file to disk because I didn’t realize you could stream an image variable to the browser, but my other co-worker Jim came to the rescue with the cfimage “writeToBrowser” action!

<cfset params = listToArray(cgi.query_string,"x") />

<cfif arrayLen(params) lt 2>
	<cfthrow message="Input should be like ?[w]x[h]"/>
</cfif>

<cfset width  = params[1] />
<cfset height = params[2] />
<cfset color  = "gray" />

<cfif not (isNumeric(width) and isNumeric(height))/>
	<cfthrow message="Width/Height should be numeric ?100x100">
</cfif>

<cfset image = ImageNew("", width, height,"rgb", color) />
<cfset ImageDrawText(image, "#width# x #height#", 0, 10)>
<cfimage source="#image#" action="writeToBrowser"/>

I’d love to link an example to you, but my hosting plan doesn’t support _cf_image_