Convert an image from CFHTTP filecontent to binary data with Coldfusion

对着背影说爱祢 提交于 2019-12-17 16:50:58

问题


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.


回答1:


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.




回答2:


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() />


来源:https://stackoverflow.com/questions/1536815/convert-an-image-from-cfhttp-filecontent-to-binary-data-with-coldfusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!