function scaleMe(theImg, maxW, maxH) { var orgW = theImg.width; var orgH = theImg.height; var ratio = (maxW/maxH); if(orgW*ratio>orgH*ratio){ theImg.width = orgW*ratio } else { theImg.height = orgH*ratio; } theImg.style.display=''; }