【问题标题】:crop an image from the centre using coldfusion使用冷融合从中心裁剪图像
【发布时间】:2011-01-09 19:04:08
【问题描述】:

这是我很长一段时间以来的第一次编程,所以我基本上是从零开始,我正在使用coldfusion 8。

我要做的是从各种较大的图像(一些肖像,一些风景)中创建一系列统一的缩略图图像(始终为 68 X 46)。在这两种情况下,调整图像大小以填充缩略图的高度或宽度,然后将多余的图像从任一侧(上/下,左/右)平均裁剪。就像 Photoshop 默认情况下调整画布大小一样。

只要源图像的尺寸/比例完美,下面的代码就可以很好地工作,但是我已经开始遇到代码失败的情况。在这种情况下,当调整大小的图像宽度最终小于 68 时。

<cfif FileExists(ExpandPath('images/gallery/thumbs/thm_'&imageMed[i].medium.XmlText)) IS false> <!--- If the thumb doesn't exist, create it. --->
<cfif imageDataThumb.width gt imageDataThumb.height >
  <!--- Landscape --->
  <cfset ImageResize(cfImageThumb,"","46")>
  <cfset ImageCrop(cfImageThumb,(cfImageThumb.width-68)/2,0,68,46)> <!--- Crop left/right edges of images --->
  <cfimage source="#cfImageThumb#" action="write" destination="images/gallery/thumbs/thm_#imageMed[i].medium.XmlText#" overwrite="yes">
<cfelse>
  <!--- Portrait --->
  <cfset ImageResize(cfImageThumb,"68","")>
  <cfset ImageCrop(cfImageThumb,0,(cfImageThumb.height-23)/2,68,46)>
  <!--- Crop top/bottom edges of images --->
  <cfimage source="#cfImageThumb#" action="write" destination="images/gallery/thumbs/thm_#imageMed[i].medium.XmlText#" overwrite="yes">
</cfif>

试图解决这些“边缘情况”会使代码变得一团糟。有没有更好的方法来解决这个问题?冷熔或 CFC 中的东西?

【问题讨论】:

    标签: image coldfusion thumbnails crop


    【解决方案1】:

    我知道这是旧的,但您可以在 ben nadel 的 imageUtils 库中使用 AspectCrop。检查 riaforge.com。它完全符合您的要求。我知道,我写了。 :)

    【讨论】:

      【解决方案2】:

      我会先检查图像的比例并相应地调整短边裁剪。为了简洁起见,我使用 CFScript,但这很容易转换为 Coldfusion 标签。

      <cfscript>
        minimumRatio = 68 / 46;   // 1.478 (68 pixels / 46 pixels)
        width = ImageGetWidth(imageDataThumb);
        height = ImageGetHeight(imageDataThumb);
      
        // determine the longside and the aspect
        if (width GT height) {
            longside  = width;
            shortside = height;
            aspect    = "landscape";
      
        } else {
            longside  = height;
            shortside = width;
            aspect    = "portrait"; 
        }
      
        // determine the aspect ratio
        if (longside / shortside GTE minimumRatio) {
            // Do normal resize / crop
      
            if (width EQ longside) {
                // landscape
      
      
            } else {
                // portrait
      
            }
      
        } else {
            // ratio is too small - perform some additional calculations before resize / crop
            // in this case, you'll likely need to resize for the opposite side then crop the excess
      
        }
      
      </cfscipt>
      
      <cfimage source="#cfImageThumb#" action="write" destination="images/gallery/thumbs/thm_#imageMed[i].medium.XmlText#" overwrite="yes">
      

      如果您不想检查比率,则在使用类似逻辑调整大小之前验证长度和宽度是否足够。

      【讨论】:

        【解决方案3】:

        代码不仅需要关注当前的比例,还需要关注如何随作物改变比例。

        此外,高度计算基于所需高度的一半,应该是全高。

        假设您的代码的所有其他内容都正确,您应该能够将下面的代码放入 FileExists if 块中,替换当前内容。

        <cfset CurrentWidth = imageDataThumb.width>
        <cfset CurrentHeight = imageDataThumb.height>
        <cfset CurrentRatio = CurrentWidth / CurrentHeight>
        <cfset DesiredRatio = 68 / 46>
        
        <cfif CurrentWidth GTE CurrentHeight>
         <!--- Landscape Image --->
         <cfif CurrentRatio LT DesiredRatio>
          <!--- More Landscape --->
          <cfset Keep = "width">
         <cfelse>
          <!--- Less Landscape --->
          <cfset Keep = "height">
         </cfif>
        <cfelse>
         <!--- Portrait Image --->
         <cfif CurrentRatio GT DesiredRatio>
          <!--- More Portrait --->
          <cfset Keep = "height">
         <cfelse>
          <!--- Less Portrait --->
          <cfset Keep = "width">
         </cfif>
        </cfif>
        
        <cfif Keep EQ "width">
          <!--- Crop top/bottom edges of images --->
          <cfset ImageResize(cfImageThumb,"68","")>
          <cfset ImageCrop(cfImageThumb,0,(cfImageThumb.height-46)/2,68,46)>
        <cfelse>
          <!--- Crop left/right edges of images --->
          <cfset ImageResize(cfImageThumb,"","46")>
          <cfset ImageCrop(cfImageThumb,(cfImageThumb.width-68)/2,0,68,46)>
        </cfif>
        
        <cfimage source="#cfImageThumb#" action="write" destination="images/gallery/thumbs/thm_#imageMed[i].medium.XmlText#" overwrite="yes">
        

        【讨论】:

        • 我刚刚将您的代码拖入其中,效果非常好,您为我省去了一大堆的麻烦。我最大的感激之情
        【解决方案4】:

        没有。 ImageScaleToFit 将确保图像适合边界,在这种情况下会留下空白。我需要用图像完全填充可用空间。

        【讨论】:

          猜你喜欢
          • 2013-08-03
          • 1970-01-01
          • 2014-05-26
          • 2019-05-13
          • 1970-01-01
          • 2011-06-10
          • 2020-09-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多