– Common Function Library Project

imageScale(selector, oldHeight, oldWidth, newParameter)

Last updated October 13, 2006


Greg Nettles

Version: 1 | Requires: CF5 | Library: UtilityLib

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.


<cfset oldWidth = 660>
<cfset oldHeight = 495>
<!--- scale by height --->
<cfset newHeight = 90>
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>

<!--- scale by width --->
<cfset newWidth = 120>
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>


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 ( 
 * @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));
blog comments powered by Disqus


Latest Additions

Kevin Cotton added
May 5, 2016

Raymond Camden added
April 25, 2016

Chris Wigginton added
January 18, 2016

Gary Stanton added
November 19, 2015

Sebastiaan Naafs - van Dijk added
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson