首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在coldfusion中将图像作为二进制流写入浏览器?

如何在coldfusion中将图像作为二进制流写入浏览器?
EN

Stack Overflow用户
提问于 2011-04-12 22:29:23
回答 5查看 10.2K关注 0票数 7

我在Coldfusion9服务器上有一项服务,可以为我们即时创建图像横幅。一台单独的机器必须使用以下内容保存这些文件:

代码语言:javascript
运行
复制
wget http://myserver.com/services/local/bannerCreator/250x250-v3.cfm?prodID=3&percentSaving=19

问题是,我想不出如何让coldfusion在不使用临时文件的情况下写出二进制数据。此时图像只是显示为一个图像标记,如下所示:

代码语言:javascript
运行
复制
<cfimage action = "writeToBrowser" source="#banner#" width="#banner.width#" height="#banner.height#" />

有什么想法吗?或者我应该只使用临时文件?

EN

回答 5

Stack Overflow用户

发布于 2011-04-12 22:57:52

我不能测试,因为你没有给出你的图像是如何生成的示例代码,但是你有没有尝试过这种方式呢?

代码语言:javascript
运行
复制
<cfcontent reset="true" variable="#imageData#" type="image/jpg" />

更新:所以我继续创建了我自己的图像;我假设您也在做类似的事情。这对我来说非常有效:

代码语言:javascript
运行
复制
<cfset img = imageNew("",200,200,"rgb","red") />
<cfcontent variable="#toBinary(toBase64(img))#" type="image/png" reset="true" />

这不需要写入文件,也不需要使用虚拟文件系统("ramdisk")

票数 12
EN

Stack Overflow用户

发布于 2011-12-16 01:41:58

如果你有一个文件/图像的二进制字节,你可以用它的内容替换输出缓冲区,如下所示:

代码语言:javascript
运行
复制
<cfscript>
// eg. this is how you get a file as a binary stream
// var fileBytes = fileReadBinary( '/path/to/your/file.jpg' );

// get the http response
var response = getPageContext().getFusionContext().getResponse();

// set the appropriate mime type
response.setHeader( 'Content-Type', 'image/jpg' );

// replace the output stream contents with the binary
response.getOutputStream().writeThrough( fileBytes );

// leave immediately to ensure no whitespace is added
abort;
</cfscript>

在使用reset="true"时,<cfcontent>所做的工作与您使用reset="true"时所做的差不多

<cfcontent>相比,这种方法的优点是我们可以在基于cfcs的cfscript中编写它。

票数 5
EN

Stack Overflow用户

发布于 2012-05-29 17:43:46

我找到了上面的解决方案

代码语言:javascript
运行
复制
<cfcontent variable="#toBinary(toBase64(img))#" type="image/png" reset="true" />

对我来说不是很有效。

设置type="image/png“只是设置响应的mime类型。我不认为它必须将图像编码为PNG。因此,与<cfimage action = "writeToBrowser"...>方法相比,生成透明的png (图像类型"argb")给我带来了奇怪的颜色。

我想,不知何故,我需要显式地将图像数据编码为PNG并直接输出二进制数据。

通过对底层java的深入研究,我想到了这一点,到目前为止,这对我来说似乎是可行的。

此示例绘制了一个带有黑色圆圈的透明png。

代码语言:javascript
运行
复制
<!--- create the image and draw it --->
<cfset img = ImageNew("", 23, 23, "argb")>
<cfset ImageSetDrawingColor(img, "black")>
<cfset ImageDrawOval(img, 0, 0, 21, 21, true)>

<!--- get the response object --->
<cfset response = getPageContext().getFusionContext().getResponse()>
<!--- set the response mime type --->
<cfset response.setHeader('Content-Type', 'image/png')>
<!--- get the underlying image data --->
<cfset bImage = ImageGetBufferedImage(img)>
<!--- get the magical object to do the png encoding --->
<cfset ImageIO = createObject("java", "javax.imageio.ImageIO")>
<!--- encode the image data as png and write it directly to the response stream --->
<cfset ImageIO.write(bImage, "png", response.getResponse().getOutputStream())>

我希望这对某些人有帮助!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5636874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档