Saturday, April 5, 2008

ColdFusion 8 - ImageResize is Losing It

An adobe forum member, that goes by the nickname dbldutch, mentioned a problem they were having with ImageResize. In some cases the resized image is off by 1 pixel. To demonstrate, take the following example (by dbldutch). Create a test image that is 458 x 229. Then try and resize it to 100 x 50.


<cfscript>
img = ImageNew( "", 458, 229, "rgb", "##d8bfd8" );
ImageSetDrawingColor( img, "##000000" );
ImageSetAntialiasing( img, "on" );
attr = { font="Arial", size="24", style="bold" };
ImageDrawText( img, "Out of my mind. Back in five minutes", 15, 125, attr);
ImageResize( img, 100, 50 );
</cfscript>

<cfdump var="#ImageInfo( img )#" label="Resized ImageInfo">


If you view the dimensions of the new image, the width and height are 1 pixel smaller than they should be.



It appears to be a rounding problem. Christian K├╝pers mentioned having similar issues on Ben Nadel's blog. It is now listed as a Known Issue under the ColdFusion 8 Update 1 release notes.

ISSUE 70069:
Sometimes the ImageResize function produces an image 1 pixel shorter or narrower than the requested size. The image does resize properly if you specify bicubic, bilinear, nearest, or highestPerformance as the interpolation parameter value.



However, adding the recommended interpolation parameters does not always work. In the example below the final height is 63, instead of the requested height of 64. Though I think "highestPerformance" is the default anyway. So I was not really expecting it to work. Perhaps the addition of "highestPerformance" in the interpolation types is just a typo.



<cfscript>
img = ImageNew( "", 472, 243, "rgb", "##d8bfd8" );
ImageSetDrawingColor( img, "##000000" );
ImageSetAntialiasing( img, "on" );
attr = { font="Arial", size="24", style="bold" };
ImageDrawText( img, "Out of my mind. Back in five minutes", 15, 125, attr);
ImageResize( img, 114, 64, "highestQuality" );
</cfscript>

<cfdump var="#ImageInfo( img )#" label="Resized ImageInfo">


As you can see the resulting height is still 1 pixel short. The height = 63, instead of 64.



Since it appears to be a rounding problem, what may also work is incrementing the width and height with a small value like "0.0001". However, I have not done extensive testing. So I am uncertain if the math works out in every case.


<cfscript>
img = ImageNew( "", 472, 243, "rgb", "##d8bfd8" );
ImageSetDrawingColor( img, "##000000" );
ImageSetAntialiasing( img, "on" );
attr = { font="Arial", size="24", style="bold" };
ImageDrawText( img, "Out of my mind. Back in five minutes", 15, 125, attr);
ImageResize( img, 114.0001, 64.0001 );
</cfscript>

<cfdump var="#ImageInfo( img )#" label="Resized ImageInfo">


If anyone has any other alternatives, I would love to hear them.

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep