<script> function resizeImg(){ var imgElement = document.getElementById('imgElement'); var iWidth = 180; var iHeight = 140; if(imgElement){ if(imgElement.width/imgElement.height>=iWidth/iHeight){ if(imgElement.width>iWidth){ imgElement.height = imgElement.height*iWidth/imgElement.width; imgElement.width = iWidth; } }else{ if(imgElement.height>iHeight){ imgElement.width=imgElement.width*iHeight/imgElement.height; imgElement.height=iHeight; } } } } </script>