CFLib.org – Common Function Library Project

imageResizeAspectRatio(originalWidth, originalHeight, target)

Last updated February 22, 2006

author

Christopher Jordan

Version: 1 | Requires: CF5 | Library: UtilityLib

Description:
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 CFLib.org which returns an image's width and height.

Return Values:
Returns a structure.

Example:

<CFSet Image = imageResizeAspectRatio(400,300,300)>
<CFOutput>
    #Image.width#<br>
    #Image.height#<br>
</CFOutput>

or... using the ImageSize UDF...

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

Parameters:

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 (cjordan@placs.net) 
 * @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;        
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson