ColdFusion升级至最新版本后CFImage处理二进制图片报错问题
ColdFusion升级后cfimage处理二进制图片报错解决方案
问题详情
升级ColdFusion至最新版本后,执行原CFML代码处理查询返回的二进制图片字段时,cfimage标签触发错误,错误提示:
读取图片时发生异常。读取PNG图片数据错误,文件序列异常。
报错代码行:
<cfimage source="#CreatedBThumbnail.GroupImageContent#" action="resize" isBase64="yes" width="200" height="200" destination="#ThePath##CreatedBThumbnail.ID#.png" overwrite="yes">
原完整代码:
<!--- 处理用户数据 ---> <cfoutput query="CreatedBThumbnail"> <cfif #IsBinary(CreatedBThumbnail.GroupImageContent)#> <cfimage source="#CreatedBThumbnail.GroupImageContent#" action="resize" isBase64="yes" width="200" height="200" destination="#ThePath##CreatedBThumbnail.ID#.png" overwrite="yes"> <cfelse> <!--- 复制默认图片 ---> <cffile action = "copy" source="#application.basePath#\Assets\images\groups_32.png" destination="#ThePath##CreatedBThumbnail.ID#.png"> </cfif> </cfoutput>
报错原因
最新版ColdFusion对cfimage的参数校验更严格:原代码传入的是二进制图片数据,但错误设置了isBase64="yes"参数(该参数用于告知cfimage source是Base64编码的字符串),参数类型不匹配导致解析失败。
解决方法
方法1:直接修正参数(最简方案)
移除isBase64="yes"参数,因为source传入的是二进制对象,无需Base64解码:
<cfimage source="#CreatedBThumbnail.GroupImageContent#" action="resize" width="200" height="200" destination="#ThePath##CreatedBThumbnail.ID#.png" overwrite="yes">
方法2:增加异常处理(更健壮)
如果数据库中可能存在损坏的二进制图片数据,可通过ImageRead()预校验并捕获异常,避免程序中断:
<cfoutput query="CreatedBThumbnail"> <cfif IsBinary(CreatedBThumbnail.GroupImageContent)> <cftry> <!--- 先验证二进制数据是否为有效图片 ---> <cfset imgObj = ImageRead(CreatedBThumbnail.GroupImageContent)> <cfimage source="#imgObj#" action="resize" width="200" height="200" destination="#ThePath##CreatedBThumbnail.ID#.png" overwrite="yes"> <cfcatch type="any"> <!--- 图片损坏时改用默认图 ---> <cffile action="copy" source="#application.basePath#\Assets\images\groups_32.png" destination="#ThePath##CreatedBThumbnail.ID#.png"> </cfcatch> </cftry> <cfelse> <cffile action="copy" source="#application.basePath#\Assets\images\groups_32.png" destination="#ThePath##CreatedBThumbnail.ID#.png"> </cfif> </cfoutput>
内容的提问来源于stack exchange,提问作者Mike Savoy
相关产品推荐
相关产品推荐

