7

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?

4

5 回答 5

13

我无法测试,因为您没有提供任何有关如何生成图像的示例代码,但是您是否尝试过类似的方法?

<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-04-12T14:57:51.733 回答
6

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

<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>

几乎什么<cfcontent>时候你使用reset="true"

这种方法的优势<cfcontent>在于我们可以将它写在基于 cfscript 的 cfcs 中。

于 2011-12-15T17:41:58.197 回答
4

我找到了上面的解决方案

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

我希望对某人有所帮助!

于 2012-05-29T09:43:46.057 回答
3

取出高度和宽度属性,添加格式属性:

<cfimage action = "writeToBrowser" source="#banner#" format="png" />

wget 应该尊重 CF 在 CFFileServlet 文件夹中创建的物理文件的重定向,但如果没有,您可以设置一个标志来制作它--max-redirect=10

正如您所建议的,临时文件也可以。只需编写文件并使用 cfheader 和 cfcontent 将其写出。只要确保使临时文件名更加独特。

<cfimage action="write" destination="tempfile.png" source="#banner#" format="png" />
<cfheader name="content-disposition" value="attachment; filename=banner.png" />
<cfcontent file="tempfile.png" deletefile="true" type="image/png" />
于 2011-04-12T15:01:02.307 回答
0

我做了类似的事情,你需要结合使用标签和cfscript。有一些图像功能是必需的。一旦你在内存中有一个图像作为变量,就有很多图像函数可用。您可以使用 CFFILE 或 CFHTTP 或许多其他方式将图像放入内存。

在我的例子中,我使用 CFIMAGE 标签将图像文件读入内存,然后通过在底部添加文本来使用 CFSCRIPT 图像函数对其进行操作,然后将生成的图像作为 .png(或如果您愿意,可以为 .jpg)输出到浏览器. 服务器上存储的唯一文件是原始图像文件。在您的情况下,您可以像以前那样使用 cfhttp 标签来调用它,而不是读取图像。这是我的代码:

<!---Get the image for processing ...--->
<cfimage action="read" source="#application.approotABS#\webcam\webcam.jpg" name="CamImage" /> 
<!--- prepare the text for overlay --->
<cfscript>
  attr=structNew();
  attr.font = "Arial";
  attr.size = 15;
  ImageSetDrawingColor(CamImage, "white");
  ImageDrawText(CamImage, 'LIVE FROM STUDIO 1', 18,(ImageGetHeight(CamImage)-54), attr);
  ImageDrawText(CamImage, '#ShowOnNow.showname#', 18,(ImageGetHeight(CamImage)-36), attr);
  ImageDrawText(CamImage, datestring,18,(ImageGetHeight(CamImage)-18), attr);

</cfscript>

<!--- further down the page, output the manipulated image: ---->
 <div class="webcam">  
      <cfimage action="writeToBrowser" source="#Camimage#"  >
  </div>

您可以在http://hawkesburyradio.com.au/index.cfm?pid=111538上看到它的实际效果

(我将 CF9 与 Windows Server 一起使用)

于 2014-08-06T11:33:47.440 回答