【问题标题】:Crop images from bottom and top equally with ColdFusion 10使用 ColdFusion 10 从底部和顶部均匀裁剪图像
【发布时间】:2013-09-14 13:27:13
【问题描述】:

我一直在尝试使用 ColdFusion 10 以编程方式调整图像大小和裁剪图像。让我抓狂的是,我无法在保持相同宽度的同时从底部和顶部同样裁剪图像。

这是我目前拥有的,只有几行简单的代码:

    <cfimage source="images/test/airateapple.png" name="myImage" overwrite="yes">

    <cfif ImageGetWidth(myImage) gte 1024>
        <cfset ImageSetAntialiasing(myImage,"on")>
        <cfset ImageScaleToFit(myImage,800,"","mediumquality")>
        <cfif ImageGetHeight(myImage) gt 350> 
            <cfset sizeToCrop= ImageGetHeight(myImage) - 350>
            <cfset ImageCrop(myImage,0, sizeToCrop
                               , ImageGetWidth(myImage)
                               , ImageGetHeight(myImage) )>
        </cfif>

        <cfset finalImage=myImage>
    </cfif>

    <!--- Display the modified image in a browser. --->
    <cfimage source="#finalImage#" action="writeToBrowser">

例如,如果调整大小后图像高度为 500 像素,则应额外裁剪 150 像素。更具体地说,从底部裁剪 75px,从顶部裁剪 75px。有可能吗?

【问题讨论】:

  • 仅供参考:我删除了代码中无关的 # 标志。唯一需要它们的地方是&lt;cfimage source="#finalImage#" ...&gt;(因为变量用引号括起来)。

标签: coldfusion coldfusion-10 cfimage


【解决方案1】:
<cfset sizeToCrop= ImageGetHeight(myImage) - 350>
<cfset ImageCrop(myImage, 0 
                     , #sizeToCrop# 
                     , #ImageGetWidth(myImage)#
                     , #ImageGetHeight(myImage)#
                   )>

如果您输出参数,您可以看到您的y 和height 值已关闭。假设原始图像尺寸为500px x 500px。现在,您开始裁剪太低(即y=150px)并使用原始 高度而不是所需高度(即350px)。

       // current code (wrong)
       ImageCrop(myImage, 0 , 150 , 500 , 500 )

要抓取图像的中心,您需要从y=75 开始裁剪(即超出高度/2)。然后使用期望的高度(即350px),而不是原来的:

       // ImageCrop( img, x, y, width, height )
       yPosition  = (originalHeight - desiredHeight) / 2;
       ImageCrop(myImage, 0, yPosition, originalWidth, desiredHeight );

【讨论】:

  • 我知道计算有些不对劲,虽然我从来不喜欢动手算术 :) 谢谢,它就像一个魅力!
猜你喜欢
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-18
相关资源
最近更新 更多