CFLib.org – Common Function Library Project

imageScale(selector, oldHeight, oldWidth, newParameter)

Last updated October 13, 2006

author

Greg Nettles

Version: 1 | Requires: CF5 | Library: UtilityLib

Description:
Returns a width or height of an image when scaled down to a specified height or width while maintaining the original aspect ratio.

Return Values:
Returns a number.

Example:

<cfset oldWidth = 660>
<cfset oldHeight = 495>
<!--- scale by height --->
<cfset newHeight = 90>
<cfoutput>
Scale by Height<br>
<cfset newWidth = imageScale('h',oldHeight,oldWidth,newHeight)>
Old Dimensions: #oldWidth# x #oldHeight#<br>
New Dimensions: <strong>#newWidth#</strong> x #newHeight#<br><br>
</cfoutput>

<!--- scale by width --->
<cfset newWidth = 120>
<cfoutput>
Scale by Width<br>
<cfset newHeight = imageScale('w',oldHeight,oldWidth,newWidth)>
Old Dimensions: #oldWidth# x #oldHeight#<br>
New Dimensions: #newWidth# x <strong>#newHeight#</strong><br>
</cfoutput>

Parameters:

Name Description Required
selector Dimension to scale. Either h (for height) or w (for width). Yes
oldHeight Old height. Yes
oldWidth Old width. Yes
newParameter New size. Yes

Full UDF Source:

/**
 * Returns width or height of a scaled image.
 * 
 * @param selector      Dimension to scale. Either h (for height) or w (for width). (Required)
 * @param oldHeight      Old height. (Required)
 * @param oldWidth      Old width. (Required)
 * @param newParameter      New size. (Required)
 * @return Returns a number. 
 * @author Greg Nettles (gregnettles@hotmail.com) 
 * @version 1, October 18, 2006 
 */
function imageScale(selector,oldHeight,oldWidth,newParameter) {
        var ratioA = oldHeight/oldWidth;
        var ratioB = oldWidth/oldHeight;
        if (selector is 'h') {
            return round(newParameter * (1/ratioA));
        } else if (selector is 'w') {
            return round(newParameter * (1/ratioB));
        }
    }

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