Created
January 8, 2014 02:43
-
-
Save pandauxstudio/8310908 to your computer and use it in GitHub Desktop.
Basic image cropping.
This file contains 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
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