如何实现限制网页图片尺寸?

来源:百度知道 编辑:UC知道 时间:2024/06/12 07:08:33
比如网站里面的图片最大不能超过
宽:550 长不限制
现在就是希望能有代码可以让宽超过550的图片自动调整到550且与原来图片等比例。高手帮忙好吗?小菜我先谢谢大家了……我要的是具体*
我指的是我上传的图片,然后前台显示出的图片尺寸……
我指的是实现,如果是传规格的图片我就不用再去实现了。我现在所求的就是那个实现代码。而不是如何设置……祝大家新年快乐……

楼主,你要的代码如下:
<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>= 140/180){
if(image.width>140){
ImgD.width=140;
ImgD.height=(image.height*140)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
/*ImgD.alt=image.width+"×"+image.height;*/
}
else{
if(image.height>180){
ImgD.height=180;
ImgD.width=(image.width*180)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
/*ImgD.alt=image.width+"×"+image.height;*/
}
}
/*else{
ImgD.src="";
ImgD.alt=""
}*/
}
</script>

把以上这段代码加在<head></head>之间
然后在图片显示的时候,用这种方式: