How do you write a image to browser as a binary stream in coldfusion?

boodle picture boodle · Apr 12, 2011 · Viewed 9.9k times · Source

I have a service on a Coldfusion 9 server that creates image banners on the fly for us. A separate machine has to save these files with something like:

wget http://myserver.com/services/local/bannerCreator/250x250-v3.cfm?prodID=3&percentSaving=19

The problem is that I can't think of how to get coldfusion to write out binary data without using a temporary file. At the minute the image is just displayed as an image tag like this:

<cfimage action = "writeToBrowser" source="#banner#" width="#banner.width#" height="#banner.height#" />

Any ideas? Or should I just use a temporary file?

Answer

Adam Tuttle picture Adam Tuttle · Apr 12, 2011

I can't test because you're not giving any example code for how your images are generated, but have you tried something along this line?

<cfcontent reset="true" variable="#imageData#" type="image/jpg" />

Update: So I went ahead and created my own image; I'll assume you're doing something similar. This works perfectly for me:

<cfset img = imageNew("",200,200,"rgb","red") />
<cfcontent variable="#toBinary(toBase64(img))#" type="image/png" reset="true" />

This works without writing to file, and without using a virtual file system ("ramdisk")