如何使用CFImage调整大小并实际减少图像文件大小?

时间:2012-07-13 11:53:34

标签: image coldfusion resize cfimage

  

可能重复:
  Why should I resize an image in Coldfusion if the file size doen't decrease and the quality of the image suffers?

我必须在这里做错事......使用Coldfuison8

我有一个产品搜索,它从外部服务器提取图像,所以我需要在运行时设置图像。

说我有一个JPG 500x500px 111kb,我从外部服务器上抓取。

现在我正在这样做:

<cfimage name="myImage" source="#bildpfad##bilddateiname#" action="read" />
<cfif IsImage(myImage) is true>
    <cfscript>
    ImageSetAntialiasing(myImage,"on");
    variables.breite = 400;
        ImageScaleToFit(myImage, variables.breite,"", "highestPerformance");
    </cfscript>
<cfxml variable="imageXml">
    <cfimage action="writetobrowser" source="#myImage#" />
</cfxml>
<cfoutput><div class="resultsImgWrap">#imageXml#</div></cfoutput>

这显示图像正常,但当我检查文件大小时,我有

400x400px 111.2kB

如果我以IrfanView为例,只是减少文件大小并保存图像,我已经达到了65kb。

问题: 如果我正在调整图像大小,如何使CFIMAGE减小文件大小?如果大小保持在111kb,那么为什么要使用CFIMAGE而不是简单地将图像添加为纯HTML。

**编辑 '':
变量Bildpfad和Bilddateiname将类似于:

bildpfad (path to image):         http://www.some-server.com/images/
bilddateiname (filename):         some_image123.jpg

3 个答案:

答案 0 :(得分:2)

quality中指定<cfimage>属性。我不确定它是否适用于action="writeToBrowser",但它应该。否则,请提交错误报告。

调整大小和压缩是不同的事情。 :)

请参阅:Why should I resize an image in Coldfusion if the file size doen't decrease and the quality of the image suffers?

<强>更新

好消息是,有一种无证的方式让它运转起来!请参阅:http://www.bennadel.com/blog/2405-Using-The-Quality-Attribute-With-The-CFImage-WriteToBrowser-Action.htm

<!--- Write out as a JPG at 20% (using "quality" attribute). --->
<cfimage
    action="writeToBrowser"
    source="#png#"
    format="jpg"
    quality=".2"
    />

答案 1 :(得分:1)

调整图像大小不是为了使文件大小以字节为单位。它的高度和宽度要小一些。是的,通常情况下,图像文件的大小会变小。但是尺寸减小取决于源图像。

您可能希望将格式声明为png,因为这可能会使文件变小。

另外,为什么要在cfxml中包装cfimage标记?这是一个无用的步骤,导致额外的处理和膨胀。

答案 2 :(得分:1)

使用'action =“writetobrowser”'将使用源图像的副本。它会创建一个临时文件,推送到浏览器并在以后销毁。

以下是CF 8文档中的一句话:

“使用writeToBrowser操作直接向浏览器显示一个或多个ColdFusion图像,而无需将其写入文件。”

我使用带有大照片(4608x3456)@ 3.47MB的CF8测试了您的代码。我浏览器上显示的图像大小调整为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>

通过将高度作为空字符串,它将调整大小并缩放高度以按比例匹配宽度。

您必须从您的网站网站根目录创建'/ images / resized /'文件夹。

ExpandPath()比使用静态路径定义更好。如果您必须将站点移动到其他服务器或操作系统,则可以节省大量时间。

注意:我更新了上面的代码,以便更好地适应问题中变量的使用。我还改变了图像的阅读方式。我安装了带有CHF 4 +修补程序的CF 8.01,但是在读取远程文件时出现错误(JPG处理中出现异常。段大小将超出文件流长度)和cfimage。我的解决方案是使用cfhttp来读取图像。我还更新了代码以使用更好的命名变量“resizedImgDestination”。