* 判断图片类型
*
* @param ths
* type="file"的javascript对象
* @return true-符合要求,false-不符合
*/
function checkImgType(ths){
if (ths.value == "") {
alert("请上传图片");
return false;
} else {
if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(ths.value)) {
alert("图片类型必须是.gif,jpeg,jpg,png中的一种");
ths.value = "";
return false;
}
}
return true;
}
/*
* 判断图片大小
*
* @param ths
* type="file"的javascript对象
* @param width
* 需要符合的宽
* @param height
* 需要符合的高
* @return true-符合要求,false-不符合
*/
function checkImgPX(ths, width, height) {
var img = null;
img = document.createElement("img");
document.body.insertAdjacentElement("beforeEnd", img); // firefox不行
img.style.visibility = "hidden";
img.src = ths.value;
var imgwidth = img.offsetWidth;
var imgheight = img.offsetHeight;
alert(imgwidth + "," + imgheight);
if(imgwidth != width || imgheight != height) {
alert("图的尺寸应该是" + width + "x"+ height);
ths.value = "";
return false;
}
return true;
}
function s() { if(img)img.removeNode(true); //如果img存在的话,就删除 img=document.createElement("img"); //新建一个img对象 img.style.position="absolute"; //位置绝对 img.style.visibility="hidden"; //隐藏 img.attachEvent("onreadystatechange",orsc); //绑定事件 img.attachEvent("onerror",oe); //同上 document.body.insertAdjacentElement("beforeend",img); //插到文档里 img.src=inp.value; //图片的值等于inp的值 }
***************************************************************
<script>
var img=null;
function s()
{
alert('aa');
if(img)img.removeNode(true);
img=document.createElement("img"); //新建一个img对象
img.src=inp.value; //路径
img.style.position="absolute";
img.style.visibility="hidden";
img.attachEvent("onreadystatechange",orsc); //页面加载的时候执行这个方法
//img.onreadystatechange = orsc;
//(只要页面中出现脚本错误,就会产生 onerror 事件)绑定方法 错误的时候
img.attachEvent("onerror",oe);
document.body.insertAdjacentElement("beforeend",img);
}
function oe()
{
alert("cant load img");
}
function orsc()
{
//页面没有完全加载时 返回false (load)
if(img.readyState!="complete")return false;
//说明 :onreadystatechange 事件能辨识readyState 属性的改变。
alert("图片大小:"+img.offsetWidth+"X"+img.offsetHeight);
alert("图片尺寸:"+img.fileSize);
}
</script>
<body>
<input id=inp type="file">
<br>
<button οnclick="s()">Test</button>
</body>
*************************************************************************************
2.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·
判断图片的长宽和大小
<script language="javascript">
<!--
//检查插入是否为图片
var img=null;
function chkimg(inp)
{
if(img)img.removeNode(true);
img=document.createElement("img");
img.attachEvent("onreadystatechange",isimg);
img.attachEvent("onerror",notimg);
img.src=inp;
}
function notimg()
{
alert("您插入的不是图片,请重新选择插入");
}
function isimg()
{
show.insertAdjacentElement("BeforeEnd",img);
show1.innerHTML = "图片大小" + img.fileSize/1024 +"K<br />图片宽度"+ img.offsetWidth +"<br />图片高度"+ img.offsetHeight;
}
// -->
</script>
<BODY>
<div id="show"></div>
<div id="show1"></div>
<input type="file" name="" onpropertychange="chkimg(this.value)"/>
</BODY>
</HTML>
3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<script>
function checkImgPX( ) {
var ths = 'C:\Users\Administrator\Desktop\a.jpg';
//var ths = 'C:\Users\Administrator\Desktop\cyf.jpg';
var width = 400;
var height = 400;
alert('aa');
var img = null;
img = document.createElement("img");
document.body.insertAdjacentElement("beforeEnd", img); // firefox不行
img.style.visibility = "hidden";
img.src = ths;
var imgwidth = img.offsetWidth;
var imgheight = img.offsetHeight;
alert(imgwidth + "," + imgheight);
if(imgwidth != width || imgheight != height) {
alert("图的尺寸应该是" + width + "x"+ height);
ths.value = "";
return false;
}
return true;
}
</script>
<body>
<input id=inp type="file">
<br>
<button οnclick="checkImgPX()">Test</button>
</body>
</html>
http://book.51cto.com/art/201101/243880.htm