– Common Function Library Project

imageResizeAspectRatio(originalWidth, originalHeight, target)

Last updated February 22, 2006


Christopher Jordan

Version: 1 | Requires: CF5 | Library: UtilityLib

This function will give you the properly resized dimensions of an image. In other words, you give it the original dimensions, the new maximum size, and the UDF returns the new size numbers while maintaining the aspect ratio. This could be very handy to use with the ImageSize UDF already published on which returns an image's width and height.

Return Values:
Returns a structure.


<CFSet Image = imageResizeAspectRatio(400,300,300)>

or... using the ImageSize UDF...

<CFSet OriginalSize = imageSize("c:\inetpub\wwwroot\sample.jpg")>
<CFSet NewImageSize = imageResizeAspectRatio(OriginalSize.width,OriginalSize.height,150)>


Name Description Required
originalWidth Width of image. Yes
originalHeight Height of image. Yes
target New target size. Yes

Full UDF Source:

 * Returns the proper dimensions of an image resized to a certain maximum size.
 * @param originalWidth      Width of image. (Required)
 * @param originalHeight      Height of image. (Required)
 * @param target      New target size. (Required)
 * @return Returns a structure. 
 * @author Christopher Jordan ( 
 * @version 1, February 22, 2006 
function imageResizeAspectRatio(originalWidth,originalHeight,target){
    var width        = 0;
    var height        = 0;
    var percentage    = 0;
    var imageProperties    = structnew();

    percentage = (target / originalHeight);
    if (originalWidth gt originalHeight) { 
        percentage = (target / originalWidth);
    newWidth                = round(originalWidth * percentage);
    newHeight                = round(originalHeight * percentage);
    imageProperties.width    = newWidth;
    imageProperties.height    = newHeight;
    return imageProperties;        


Latest Additions

Raymond Camden added
November 04, 2017

Leigh added
May 11, 2016

Raymond Camden added
May 10, 2016

Kevin Cotton added
May 05, 2016

Raymond Camden added
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson