Convert an image from CFHTTP filecontent to binary data with Coldfusion
Asked Answered
E

2

6

I'm trying to convert an image (jpg) loaded via cfhttp to binary data. I can't use cffile action="readBinary" as it is not a local file.

Eugeniaeugenics answered 8/10, 2009 at 10:4 Comment(1)
Just a quick bit of Railo evangelism, re: "I can't use cffile action="readBinary" as it is not a local file." - in Railo, you can use cffile for any file, even across http and ftp. :)Ratoon
T
15

Here's how I handle this, and I use this to grab and process hundreds of images a day with ColdFusion 8.

<cfhttp
    timeout="45"
    throwonerror="false"
    url="http://domain/image.jpg"
    method="get"
    useragent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12"
    getasbinary="yes"
    result="local.objGet"
>

<cfset local.objImage = ImageNew(local.objGet.FileContent)>

Once you have the image object you can then do whatever you want with it. Save it to disk, resize, you name it :). I've obviously left out all of my error checking (200 status codes, is it an image, etc), but this should get you started.

Tracey answered 8/10, 2009 at 12:12 Comment(0)
E
5

I've done the following which seems to work:

<cfhttp url="http://foo.com/someImage.jpg" method="get" timeout="3" result="resp">
</cfhttp>

<cfreturn resp.fileContent.toByteArray() />
Eugeniaeugenics answered 8/10, 2009 at 10:28 Comment(2)
How can we write resp.fileContent.toByteArray() to a file? I mean how to write binary data to file?Somali
using Lucee you can just use resp.fileContent as this returns a true byte arrayFelly

© 2022 - 2024 — McMap. All rights reserved.