Skip to content

Instantly share code, notes, and snippets.

@pandauxstudio
Created January 8, 2014 02:43
Show Gist options
  • Save pandauxstudio/8310908 to your computer and use it in GitHub Desktop.
Save pandauxstudio/8310908 to your computer and use it in GitHub Desktop.
Basic image cropping.
function momentImgResize(img, maxWidth, maxHeight) {
if (img.data('type') == 'placeholder') {
return;
}
var widthHeightRatio = 1.5;
var imgWidth = img.width();
var imgHeight = img.height();
if ((imgWidth / imgHeight) == widthHeightRatio) {
img.css('width', maxWidth + 'px');
img.css('max-height', 'none');
} else if ((imgWidth / imgHeight) < widthHeightRatio) {
img.css('width', maxWidth + 'px');
img.css('max-height', 'none');
imgHeight = img.height();
img.css('margin-top', -( (imgHeight - maxHeight) / 2 ) + 'px');
} else {
img.css('height', maxHeight + 'px');
img.css('max-width', 'none');
imgWidth = img.css('width');
imgWidth = img.width();
img.css('margin-left', -( (imgWidth - maxWidth) / 2 ) + 'px');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment