Return to Snippet

Revision: 24360
at March 7, 2010 08:31 by kaptnkill


Updated Code
/**
* resize
* Take input diemnsion and return the resulting dimensions using a ratio 
* @param _w initial width
* @param _h initial height
* @param _targetW targeted width
* @param _targetH targeted height
* @param _ratio set the ratio manually instead of automatically detecting it (0)
* @return An object containing both resulting dimensions :object.width,object.height
*/
function resize(_w:Number,_h:Number,_targetW:Number,_targetH:Number,_ratio:Number = 0):Object {
	var ratio:Number = (_ratio ==0) ? _w / _h : _ratio;
	var rW:Number = _targetW;
	var rH:Number = _targetH;
	if (ratio >= 1) {
		rH = _targetW / ratio;
		if (rH > _targetH) {
			rH = _targetH;
			rW = rH * ratio;
		}
	}else {
		rW = _targetH * ratio;
		if (rW > _targetW) {
			rW = _targetW;
			rH = rW / ratio;
		}
	}
	var newDimensions:Object = {width:rW,height:rH };
	return newDimensions;
}

Revision: 24359
at February 27, 2010 12:13 by kaptnkill


Updated Code
/**
* resize
* Return the new dimension ,width and height 
* @param _w initial width
* @param _h initial height
* @param _targetW targeted width
* @param _targetH targeted height
* @param _ratio set the ratio manually instead of automatically detecting it (0)
* @return An object containing both resulting dimensions :object.width,object.height
*/
function resize(_w:Number,_h:Number,_targetW:Number,_targetH:Number,_ratio:Number = 0):Object {
	var ratio:Number = (_ratio ==0) ? _w / _h : _ratio;
	var rW:Number = _targetW;
	var rH:Number = _targetH;
	if (ratio >= 1) {
		rH = _targetW / ratio;
		if (rH > _targetH) {
			rH = _targetH;
			rW = rH * ratio;
		}
	}else {
		rW = _targetH * ratio;
		if (rW > _targetW) {
			rW = _targetW;
			rH = rW / ratio;
		}
	}
	var newDimensions:Object = {width:rW,height:rH };
	return newDimensions;
}

Revision: 24358
at February 27, 2010 12:09 by kaptnkill


Initial Code
/**
* resize
* Return the new dimension ,width and height 
* @param _w initial width
* @param _h initial height
* @param _targetW targeted width
* @param _targetH targeted height
* @param _ratio set the ratio instead of automatically detecting it (0)
* @return An object containing both resulting dimensions :width,height
*/
function resize(_w:Number,_h:Number,_targetW:Number,_targetH:Number,_ratio:Number = 0):Object {
			var ratio:Number = (_ratio ==0) ? _w / _h : _ratio;
			var rW:Number = _targetW;
			var rH:Number = _targetH;
			if (ratio >= 1) {
				rH = _targetW / ratio;
				if (rH > _targetH) {
					rH = _targetH;
					rW = rH * ratio;
				}
			}else {
				rW = _targetH * ratio;
				if (rW > _targetW) {
					rW = _targetW;
					rH = rW / ratio;
				}
			}
			var newDimensions:Object = {width:rW,height:rH };
			return newDimensions;
		}
	}

Initial URL

                                

Initial Description
Usage:

<code>
var newDimensions:Object = resize(800,600,320,200);
</code>

This can be adapted to pretty much any language...

Initial Title
AS3 Resize with ratio

Initial Tags
resize, image

Initial Language
ActionScript 3