Skip to content

Instantly share code, notes, and snippets.

@vanrez-nez
Created December 18, 2015 19:37
Show Gist options
  • Save vanrez-nez/defef8de6110ac9239d9 to your computer and use it in GitHub Desktop.
Save vanrez-nez/defef8de6110ac9239d9 to your computer and use it in GitHub Desktop.
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