var flag = false; function DrawImage2(ImgD) { //alert(ImgD.style.height); var image = new Image(); image.src = ImgD.src; if (image.width > 0 && image.height > 0) { flag = true; if (image.height > 150) { ImgD.height = 150; ImgD.width = (image.width * 150) / image.height; } else { ImgD.width = image.width; ImgD.height = image.height; } if(ImgD.height==150){ ImgD.style.marginLeft=(200-ImgD.width)/2+'px'; }else{ ImgD.style.marginTop=(150-ImgD.height)/2+'px'; } } }
<img src=”1.jpg” onload=”javascript:DrawImage2(this);” />