Created
May 31, 2012 22:27
-
-
Save mohamedmansour/2846772 to your computer and use it in GitHub Desktop.
FIT Algorithm for resizing photos.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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