使用 Coldfusion 从中心裁剪图像

发布于 2024-08-19 15:30:24 字数 1328 浏览 6 评论 0原文

这是我相当长一段时间以来的第一次编程,所以我基本上是从头开始,我使用的是 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>

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

This is my first bit of programming in quite a while, so I'm basically starting from scratch, and I'm using coldfusion 8.

What I'm trying to do is create a series of uniform thumbnail images (always 68 X 46) from a variety of larger images, some portrait, some landscape. In both cases resizing the image to fill the height or width of the thumbnail and then cropping the excess image equally off either side (top/bottom, left/right). Just as photoshop does by default with canvas resize.

The code below works really well as long as the source images dimensions/ratio is perfect, but I've started to run into cases where the code fails. In this case, when a resized images width ends up being less than 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>

Trying to solve these "edge cases" is turning the code into a mess. Is there a better way to approach this? Something in coldfusion or a cfc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你另情深 2024-08-26 15:30:24

我会首先检查图像的比例,并相应地调整短边裁剪。为了简洁起见,我使用 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">

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

I'd check the proportions of the image first and adjust the short side crop accordingly. I'm using CFScript for conciseness, but this could easily be converted to Coldfusion tags.

<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">

If you don't want to check for ratio, then verify the length and width is enough before resizing using similar logic.

柠檬色的秋千 2024-08-26 15:30:24

我知道这已经很旧了,但是您可以使用 ben nadel 的 imageUtils 库中的 AspectCrop 。检查 riaforge.com。它正是您所寻找的。我知道,我写的。 :)

I know this is old, but you could just use AspectCrop in the imageUtils library from ben nadel. Check riaforge.com. It does exactly what you are looking for. I know, i wrote it. :)

输什么也不输骨气 2024-08-26 15:30:24

代码不仅需要注意当前的比率,还要注意如何通过裁剪来更改比率。

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

假设您的代码的其他所有内容都是正确的,您应该能够将下面的代码放入您的 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">

The code needs to not only pay attention to the current ratio, but how you're changing the ratio with the crop.

Also, the height calculation was based on half the desired height, and should have been the full height.

Assuming all else is correct with your code, you should be able to place the code below into your FileExists if block, replacing the current content.

<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">
冰雪梦之恋 2024-08-26 15:30:24

不会。ImageScaleToFit 将确保图像适合边界内,在本例中会留下空白。我需要用图像完全填充可用空间。

No. ImageScaleToFit would makes sure the image fits inside the bounds, which in this case leaves white space. I need to completely fill the available space with image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文