Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Image resizing utils
function getSizeToCover(width, height, maxWidth, maxHeight) {
var ratio = Math.max(maxWidth / width, maxHeight / height);
return {
width: width * ratio,
height: height * ratio
};
}
function getSizeToContain(width, height, maxWidth, maxHeight) {
var ratio = Math.min(maxWidth / width, maxHeight / height);
return {
width: width * ratio,
height: height * ratio
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment