的使用的行動=‘writetobrowser’'將使用源圖像的副本。它會創建一個臨時文件,將其推送到瀏覽器並在以後銷燬。
下面是從CF 8文檔的一句話:
「使用writeToBrowser動作,而不將它們寫入文件直接顯示一個或多個圖像的ColdFusion的瀏覽器。」
我測試了你的代碼與CF8與一張大照片(4608x3456)@ 3.47MB。我的瀏覽器上顯示的圖像被調整爲400x300 @ 247.47KB。我使用Firefox右鍵單擊|查看圖像信息以獲取調整大小的圖像信息。您也可以使用Firebug |淨選項卡以獲取傳輸的圖像大小。
調整大小的圖像將位於「/ CFFileServlet/_cf_image /」中。
原始文件(#bildpfad ## bilddateiname#)的大小不會更改。
不要依賴於設計爲臨時文件的存在。暫時就是這個意思。即使它是相同的源圖像,在10分鐘後調整大小的文件名也可能不是相同的文件名。臨時文件也可以在不同版本的CF之間進行不同的處理。
如果您需要保留調整大小的圖像的副本,請使用action =「write」或action =「resize」作爲目標屬性,並將大小調整的圖像的副本保存到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 = [#bildpfad##bilddateiname#]<br />destination = [#ExpandPath(resizedImgDestination)#]<br /></cfoutput>
<!---/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 +修補程序的CF 8.01,但在讀取遠程文件時,出現錯誤(在JPG處理中發生異常,段大小超出文件流長度)與cfimage。我的解決方案是使用cfhttp來讀取圖像。我還更新了代碼以使用更好的命名變量「resizedImgDestination」。
你的數字是否正確爲11.2kB <111kb,這樣你就可以得到你想要的結果。 – nosilleg 2012-07-13 12:14:17
我想隨着任何調整圖像大小,你是不是修改圖像,只是如何查看,所以你的文件大小將永遠保持不變。 – Limey 2012-07-13 12:16:17
@nosilleg:我希望...但是它是111.2kb :-( – frequent 2012-07-13 12:19:04