我在Coldfusion9服务器上有一项服务,可以为我们即时创建图像横幅。一台单独的机器必须使用以下内容保存这些文件:
wget http://myserver.com/services/local/bannerCreator/250x250-v3.cfm?prodID=3&percentSaving=19问题是,我想不出如何让coldfusion在不使用临时文件的情况下写出二进制数据。此时图像只是显示为一个图像标记,如下所示:
<cfimage action = "writeToBrowser" source="#banner#" width="#banner.width#" height="#banner.height#" />有什么想法吗?或者我应该只使用临时文件?
发布于 2011-04-12 22:57:52
我不能测试,因为你没有给出你的图像是如何生成的示例代码,但是你有没有尝试过这种方式呢?
<cfcontent reset="true" variable="#imageData#" type="image/jpg" />更新:所以我继续创建了我自己的图像;我假设您也在做类似的事情。这对我来说非常有效:
<cfset img = imageNew("",200,200,"rgb","red") />
<cfcontent variable="#toBinary(toBase64(img))#" type="image/png" reset="true" />这不需要写入文件,也不需要使用虚拟文件系统("ramdisk")
发布于 2011-12-16 01:41:58
如果你有一个文件/图像的二进制字节,你可以用它的内容替换输出缓冲区,如下所示:
<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中编写它。
发布于 2012-05-29 17:43:46
我找到了上面的解决方案
<cfcontent variable="#toBinary(toBase64(img))#" type="image/png" reset="true" />对我来说不是很有效。
设置type="image/png“只是设置响应的mime类型。我不认为它必须将图像编码为PNG。因此,与<cfimage action = "writeToBrowser"...>方法相比,生成透明的png (图像类型"argb")给我带来了奇怪的颜色。
我想,不知何故,我需要显式地将图像数据编码为PNG并直接输出二进制数据。
通过对底层java的深入研究,我想到了这一点,到目前为止,这对我来说似乎是可行的。
此示例绘制了一个带有黑色圆圈的透明png。
<!--- 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())>我希望这对某些人有帮助!
https://stackoverflow.com/questions/5636874
复制相似问题