使用“action =”writetobrowser“”将使用源图像的副本。它创建一个临时文件,推送到浏览器并稍后销毁。
这是CF 8文档中的一句话:
“使用writeToBrowser操作直接向浏览器显示一个或多个ColdFusion图像,而无需将它们写入文件。”
我使用CF8测试了您的代码,并使用大型照片(4608x3456)@ 3.47MB。在我的浏览器上显示的图像被调整为400x300 @ 247.47KB。我使用Firefox右键单击|查看图像信息来获取调整大小的图像信息。您还可以使用Firebug | Net选项卡获取传输的图像大小。
调整大小后的图像将位于“/ CFFileServlet / _cf_image /”中。
原始文件的大小(#bildpfad##bilddateiname#)不会更改。
不要依赖于旨在是临时的文件的存在。临时意味着仅此而已。即使是相同的源图像,在10分钟后进行调整大小时,一个调整大小的文件名可能与另一个调整大小的文件名不同。不同版本的CF可能会以不同的方式处理临时文件。
如果您需要保留调整大小后的图像副本,请使用action="write"或action="resize",并使用destination属性在CF服务器上保存调整大小后的图像副本。
以下是一个示例,说明如何操作:
<!---assume that bildpfad can be different domains/folders--->
<!---RegEx = remove protocol, replace '/' with '!', replace '.' with '~'--->
<cfset slashReplacement = "!">
<cfset periodReplacement = "~">
<cfset imageFilename = REReplace(REReplace(REReplaceNoCase(bildpfad, "https?://", "", "one"), "\/", slashReplacement, "all"), "\.", periodReplacement, "all") & bilddateiname>
<!---resizedImgDestination is a web path--->
<cfset resizedImgDestination = "/images/resized/rs_#imageFilename#">
<!---debug info, can comment out when working--->
<cfoutput>source = [
<!---/debug info, can comment out when working--->
<cfif NOT FileExists(ExpandPath(resizedImgDestination))>
<cfhttp url="#bildpfad##bilddateiname#" result="stGetImg" resolveurl="no" useragent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1" timeout="45" getasbinary="yes" throwonerror="no" />
<cfset myImage = ImageNew(stGetImg.FileContent)>
<cfif IsImage(myImage)>
<cfimage action="resize" destination="#ExpandPath(resizedImgDestination)#" height="" isbase64="false" overwrite="true" source="#myImage#" width="400" />
</cfif>
</cfif>
<cfoutput><img src="#resizedImgDestination#" /></cfoutput>
通过将高度设置为空字符串,它将按比例调整高度以匹配宽度的大小并进行缩放。
您需要从站点Web根目录创建'/images/resized/'文件夹。
ExpandPath()比使用静态路径定义更好。如果您必须将站点移动到不同的服务器或操作系统,则会节省大量时间。
注意:我更新了上面的代码,以更好地适合问题中的变量使用。我还更改了图像的读取方式。我使用安装了CHF 4 + Hotfixes的CF 8.01,但在读取远程文件时,cfimage出现错误(JPG处理中发生异常。段大小将超出文件流长度)。我的解决方案是使用cfhttp来读取图像。我还更新了代码以使用更好命名的变量“resizedImgDestination”。