Skip to content

Instantly share code, notes, and snippets.

@egnedko
Forked from mohamedmansour/fit_adjust.js
Created April 24, 2017 10:58
Show Gist options
  • Save egnedko/4fc6b16e73890b0e576e0621d6e1cb5a to your computer and use it in GitHub Desktop.
Save egnedko/4fc6b16e73890b0e576e0621d6e1cb5a to your computer and use it in GitHub Desktop.
FIT Algorithm for resizing photos.
/**
* Adjust the resolution by scaling the height and width to fit the current
* width and height of the pane.
*
* @param {string} originalResolution the resolution in object format
* @param {string} maxResolutionthe resolution in object format
*
* @author Mohamed Mansour (http://mohamedmansour.com)
*/
var adjustResolution = function(originalResolution, maxResolution) {
var width = originalResolution.width;
var height = originalResolution.height;
var maxWidth = maxResolution.width;
var maxHeight = maxResolution.height;
// Check if the current width is larger than the max
if (width > maxWidth) {
var ratio = maxWidth / width;
height = height * ratio;
width = width * ratio;
}
// Check if current height is larger than max
if (height > maxHeight) {
var ratio = maxHeight / height;
height = maxHeight;
width = width * ratio;
}
return {
width: width,
height: height
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment