JavaScript获取图片尺寸

方法一:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分享javascript-sharejs.com</title>
</head>
<body>

<script>
var img=null;
function s()
{
if(img)img.removeNode(true);
img=document.createElement("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;
}
function oe()
{
alert("cant load img");
}
function orsc()
{
if(img.readyState!="complete")return false;
alert("高:"+img.offsetHeight+"\n宽:"+img.offsetWidth);
}
</script>
<input type="file" Name="file" id="inp" value="默认值"><br><input onclick="s()" type="button" value="点我一下给出要上传图片的大小及长、宽" name="button">

<br><br>
</body>
</html>

方法二:
<html>  
<head>  
    <script >
 <!--  
    function QFord_getImageSize(FilePath)  
    {  
        var imgSize={  
        width:0,  
        height:0  
        };  
         image=new Image();  
        image.src=FilePath;  
        imgSize.width =image .width;  
        imgSize .height=image .height;  
        return imgSize;  
    }  
 // -->
 </script>  
</head>  
 
<body bgcolor="#ffffff">  
JS获取图片尺寸函数测试:</br>  
<img src="test.jpg" mce_src="test.jpg" />  
<script type="text/javascript">
<!--  
var a=QFord_getImageSize("test.jpg");  
alert('宽:'+a.width +'  高:'+a.height);  
// -->
</script>  
</body>  
</html> 

你可能感兴趣的:(JavaScript,html,XHTML,image,function,button)