Use <CFIMAGE> to Generate Temporary Thumbnails
I’m wrapping up a sizable project that required 200×200 thumbnails for about two thousand pieces of content. The agency I’m working with required properly-sized thumbnails to proceed with their testing. Unfortunately, thumbnails did not exist at that point.
ColdFusion <CFIMAGE> to the rescue!
By using <CFIMAGE>, I was able to generate thumbnails for every piece of content in under 30 seconds. To add a little variety into the mix I created five different base images named thumb1.jpg – thumb5.jpg and chose one at random for each record. As I looped through the table that contained the content records, I used the content record’s ID (an auto-generated integer) as the filename and updated the thumbnail field as I went along.
The code I used looks something like this:
<cfsetting requestTimeOut = "9999">
<cfquery name="qContent" datasource="myDS">
select *
from content
</cfquery>
<cfset thumbpath= ExpandPath("/images/tempthumbs/") />
<cfoutput>
<cfloop query="qContent">
<cfset thumbnum = RandRange( 1, 5, "SHA1PRNG" )>
<cfset thumbname = "thumb#thumbnum#.jpg">
<cfimage source="#thumbpath##thumbname#" name="contentThumb">
<cfset ImageDrawText(contentThumb, "#content_name#", 5, 15)>
<cfimage source="#contentThumb#" action="write" destination="#thumbpath##qContent.contentID#.jpg" overwrite="yes">
<cfquery name="updContent" datasource="myDS">
UPDATE content
SET thumbnail_image='http://www.mydomain.com/images/tempthumbs/#qContent.contentID#.jpg'
WHERE contentID = '#contentID#'
</cfquery>
</cfloop>
</cfoutput>
The “cfset imageDrawText” line is the one that adds the text contained in the content_name column to the image. It also places the caption 5px in and 15px down from the top left corner of the image.
When the code finishes I have unique thumbnails for each piece of content.
-rG
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.



Hitting the DB each time?
Hi Raul,
This code will loop through all records in the content table, one time, in a test environment. The whole process, including the image creation, takes about 30 seconds. What are your concerns?