当你在JSP页面或是HTML页面,显示图片需要控制其大小时,则可以使用以下方法:
法一:
<!-- 等比例改变显示图片的大小 -->
<script type="text/javascript">
function imgSize(ImgD,imgW,imgH){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
if(image.width/image.height>=imgW/imgH){
if(image.width>imgW){
ImgD.width=image.width;
ImgD.Height=(image.height*imgW)/image.width;
}else{
ImgD.width=image.width;
ImgD.Height=image.height;
}
}else{
if(image.height>imgH){
ImgD.width=(image.width*height)/image.height;
ImgD.height=imgH;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
</script>
<img src='hello.jpg' onload="imageSize( this , 450 , 350 )"
法二:(这是比较简单的一个方法,如果你的页面只需显示一张图片的,那么就可以用这个简便的方法)
<img src='hello.jpg' onload="javascript:if(this.width>450)this.width=450;if(this.height>350)this.height=350;" />