日期:2014-05-17  浏览次数:21108 次

关于图片超过规定尺寸就等比缩小的老问题
上传了很多图片在一个页面全部显示,由于图片尺寸不统一,显示起来会出现横向滚动条,比较难看,所以想这样处理:
如果图片的宽度超过了800,则图片以800的宽度来显示,图片的高度则在原始基础上等比缩小,图片不会变形,如果图片的宽度没有超过800,则按原始尺寸显示,现在数据表中只有图片的保存路径没有图片的宽度和高度,这个需求能实现吗?
用 <img   src= " <%=Rs( "imgrul ")%> "   onload= "this.width> this.height?this.width=800:this.height=600 "> 这样固定宽度和高度,就容易有变形的.
用JS能获得这个图片的高度和宽度吗?

------解决方案--------------------
<img src= " <%=Rs( "imgrul ")%> " onload=javascript:DrawImage(this); >
<script language= "JavaScript ">
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width> 0 && image.height> 0){
flag=true;
if(image.width/image.height> = 800/600){
if(image.width> 800){
ImgD.width=800;
ImgD.height=(image.height*800)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}

}
else{
if(image.height> 600){
ImgD.height=600;
ImgD.width=(image.width*600)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
//-->
</script>