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;		
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson