利用js獲取圖片尺寸與圖片大小(高度與寬度)


利用獲取圖片尺寸與圖片大小(高度寬度)要注意一點的是要等 圖片加載完成后才能js 獲取圖片寬度與高度的,所以要判斷在readystate=="complete"的狀態下獲取大小,如果是利用file上傳的話,每次都要點擊清除 image=new image();  imgage.width與高度哦。
<!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=gb2312" />
<title>js獲取圖片尺寸與圖片大小(高度與寬度)</title>
<script>
function checkimg(img)

{
 var message="";
 var maxwidth=1;//設置圖片寬度界限
 var maxheight=1;//設置圖片高度界限

 if(img.readystate!="complete"){

  return false;//確保圖片完全加載

 }
 if(img.offsetheight>maxheight) message+="r高度超額:"+img.offsetheight;
 if(img.offsetwidth>maxwidth) message+="r寬度超額:"+img.offsetwidth;
 if(message!="") alert(message);
}

</script>
</head>
<body>
<img src="http://jz345.net/images/frequency.gif" border=0 id="img1" onload="checkimg(this);">
<br> <input id=inp type="file" onpropertychange="img1.src=this.value;">
</body>
圖片加載完成后才能成功獲取圖片大小,所以要判斷在readystate=="complete"的狀態下獲取大小

測試代碼
<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>

js獲取圖片尺寸函數測試:</br>  
<img src="test.jpg" mce_src="test.jpg" />  
<script type="text/網頁特效"><!--  
var a=qford_getimagesize("test.jpg");  
alert('寬:'+a.width +'  高:'+a.height);
</script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM