Skip to content

Instantly share code, notes, and snippets.

@redteam-snippets
Created September 23, 2012 04:59
Show Gist options
  • Save redteam-snippets/3768927 to your computer and use it in GitHub Desktop.
Save redteam-snippets/3768927 to your computer and use it in GitHub Desktop.
Javascript: Calculate Resize Dimensions
function calculateResize (_params) {
var ratio = 0;
var output = {
width : _params.current_width,
height : _params.current_height
};
if(_params.current_width > _params.max_width){
ratio = _params.max_width / _params.current_width;
output.width = parseInt(_params.max_width, 10);
output.height = parseInt((_params.current_height * ratio), 10);
_params.current_height = _params.current_height * ratio;
_params.current_width = _params.current_width * ratio;
}
if(_params.current_height > _params.max_height){
ratio = _params.max_height / _params.current_height;
output.height = parseInt(_params.max_height, 10);
output.width = parseInt((_params.current_width * ratio), 10);
_params.current_width = _params.current_width * ratio;
}
return output;
}
var newSize = calculateResize({
current_width : 300,
current_height : 100,
max_width : 280,
max_height : 195
});
console.log(newSize);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment