日期:2014-05-16  浏览次数:20424 次

【转】前台JS限制上传图片质量大小和尺寸

前台JS限制上传图片质量大小和尺寸!(转)<html>?
<head>?
<meta http-equiv="Content-Type" content="text/html; charset=??????">?
<script language="javascript">?
<!--?
function imgSel()?
{?
var img = new Image();?
img.src = document.imageForm.file.value;?
document.imageForm.width.value = img.width;?
document.imageForm.height.value = img.height;?
document.imageForm.size.value = img.fileSize;?
document.images['image'].src = img.src;?
}?
-->?
</script>?
</head>?
<body>?
<form name="imageForm">?
宽: <input name="width" type="text" size="6"> 高: <input name="height" type="text" size="6"> 大小: <input name="size" type="text" size="6"><br>?
<input name="file" type="file" onChange="imgSel()"><br>?
<img src="" name="image">?
</form>?
</body>?
</html>?



用CSS实现的方法:?

??? 把一副大图片按比例缩小或者放大到某个尺寸,对于标准浏览器(如Firefox),或者最新都IE7浏览器,直接使用max-width,max-height;或者min-width,min-height的CSS属性即可。如:?
img{max-width:100px;max-height:100px;}?
img{min-width:100px;min-height:100px;}?

对于IE6及其以下版本的浏览器,则可以利用其支持的expression属性,在css code中嵌入javascript code来实现图片的缩放?
.thumbImage {max-width: 100px;max-height: 100px;} /* for Firefox & IE7 */?
* html .thumbImage { /* for IE6 */?
width: expression(this.width > 100 && this.width > this.height ? 100 : auto);?
height: expression(this.height > 100 ? 100 : auto);?
}?

由于把图片放大,可能存在图片锯齿化的问题,一般用在图片缩小的情况是较多的。?

-----------------------------------------------------------------------?
有时候图片太大,会破环网页整齐的布局。这时可以用css来强制按比例压缩图片的高度或宽度?
css代码如下:?
img,a img{