How to fit the image properly in div

Published on Author Code Father

Way to fit the image properly in div. Use the below Javascript for re-size the image:

window.imagefit = function (img){
var width = img.naturalWidth;
var logoHeight = img.naturalHeight;
var ans = calculateAspectRatioFit(width, logoHeight, img.clientWidth, img.clientHeight); = ans.height+’px’; = ans.width+’px’;
function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) {

var ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);

return { width: srcWidth*ratio, height: srcHeight*ratio };