ColdFusion 8: URL Thumbnails
A reader asked me this morning if ColdFusion 8 can create images from URLs. This is often used to provide a snap shot of a remote site. Turns out this is relatively easy. Damon Cooper of Adobe showed an example of this a few weeks ago. It takes all of two tags:
<cfdocument src="http://www.coldfusionjedi.com" name="pdfdata" format="pdf" />
<cfpdf source="pdfdata" pages="1" action="thumbnail" destination="." format="jpg" overwrite="true" resolution="high" scale="25">
The first line simply uses cfdocument with the src attribute. I point to a URL (in this case, my blog) and store the result in a PDF variable.
Next I use the cfpdf tag to create a thumbnail. I specify the JPG format, use a high resolution, and set a scale to 25% just for the heck of it. Also note I only do page 1. By default the cfpdf/action="thumbnail" tag will create a thumbnail for each page of the PDF, but all we really want is the first page.
That's it. Done. Complete. Simple as pie. But of course I had to go a bit crazy and make a UDF out of it. The code below allows you to pass a URL (and an optional scale). It will then handle making the image, reading it into a CF8 Image object, deleting the file, and returning the object. You can then save it, or do whatever. For my tests, I did:
<cfset myimage = getThumbnail("http://www.coldfusionjedi.com",30)>
<cfimage action="writeToBrowser" source="#myimage#">
The "writeToBrowser" action lets me test without actually saving a file, but I believe it doesn't work in IE. (Not that I care.) Enjoy, and let me know how it works for you. I'll probably add options to let you specify an image type as well.
The image quality is pretty good I think. It is not the same as what you see from Firefox, but for a thumbnail, I think it works ok:

<cffunction name="getThumbnail" returnType="any" output="false">
<cfargument name="url" type="string" required="true">
<cfargument name="scale" type="numeric" required="false" default="25">
<cfset var pdfdata = "">
<cfset var prefix = replace(createUUID(),"-","_","all")>
<cfset var myimage = "">
<!--- make the pdf --->
<cfdocument src="#arguments.url#" name="pdfdata" format="pdf" />
<!--- write out the image --->
<cfpdf source="pdfdata" pages="1" action="thumbnail" destination="." format="jpg" overwrite="true"
resolution="high" scale="#arguments.scale#" imagePrefix="#prefix#">
<!--- read it in --->
<cfset myimage = imageNew(expandPath('./#prefix#_page_1.jpg'))>
<!--- clean it up --->
<cffile action="delete" file="#expandPath('./#prefix#_page_1.jpg')#">
<cfreturn myimage>
</cffunction>
Comments
I've been wanting to be able to do that for ages!
BD has a "jpg" format on their cfdocument, I was hoping Adobe would think this was a good idea too!
I knew if there was a way you would know how.
I think the quality is prety good, and while the CFDocument might not render every element perfectly, which browser does?
If you add in some IMAGE tags to dress up the final result, crop, put a border etc, would work well in lots of situations.
I'll try it out and check out how much megahurtz it steels.
Should work in any browser.
error:Attribute validation error for tag CFDOCUMENT.
Here are a few sample URLs that get a little funky:
http://www.caraccessories.com/
http://www.partstrain.com/
Is this worthy of another blog entry?
One is support. I don't know if that is free or not. I've only ever called support when I needed help with my CS3 install (oh god, the pain, the pain!). You would use that option if you need something fixed now.
If you just want to file a bug report, then go to www.adobe.com/go/wish. You can also use that URL to request a new feature.
At least that's been my observation. Still very cool.
I just tried this, and using the exact code in your eg there im seeing this exception
writeToBrowser:
coldfusion.image.Image$ReadImageMetadataException: Unable to read image source properly. D:\Domains\xxxxxx.com\wwwroot\0DBCEC5F_19B9_F849_B29064C59F3AC984_page_1.jpg (The system cannot find the file specified)
any ideas why this wouldnt work?
<cfreturn imageGetBlob(myimage)>
This makes the UDF return the binary data of the image, not a 'native cf image' which is barfing when you try to use it. It barfs because we delete the original temp source we used. As I said, something must have changed in 801.
Anyway, the binary data CAN be used with writeToBrowser, or you can save it, whatever. Either way, it's fixed now.
Always find your blogs useful. A related question to this thread - whats the easiest way to convert CFML output or text to an image in CF?
Tim
