1

これはかなり久しぶりのプログラミングなので、基本的にゼロから始めて、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 の何か?

4

4 に答える 4

2

これが古いことは知っていますが、ben nadel の imageUtils ライブラリで AspectCrop を使用することもできます。riaforge.com をチェックしてください。それはまさにあなたが探していることをします。知ってます、書きました。:)

于 2010-08-23T18:39:34.623 に答える
1

最初に画像の縦横比を確認し、それに応じて短辺のトリミングを調整します。簡潔にするために 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">

比率を確認したくない場合は、同様のロジックを使用してサイズを変更する前に、長さと幅が十分であることを確認してください。

于 2010-01-27T15:20:10.463 に答える
1

コードは、現在の比率だけでなく、トリミングで比率をどのように変更しているかにも注意を払う必要があります。

また、高さの計算は目的の高さの半分に基づいており、完全な高さである必要がありました。

他のすべてがコードで正しいと仮定すると、以下のコードを 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">
于 2010-02-01T02:03:03.470 に答える
-2

いいえ。 ImageScaleToFit は、画像が境界内に収まるようにします。この場合、空白が残ります。利用可能なスペースを画像で完全に埋める必要があります。

于 2010-01-27T14:39:46.600 に答える