Get the length and width of a picture
| code is as follows |
copy code |
<script> function GetImageSize (imge) { var i = new Image () ; Create a new Picture object i.src = imge.src;// Assign the Src property of a picture to the SRC for the new picture object return new Array (i.width,i.height); } </script> |
Call Mode:
| The code is as follows |
Copy Code |
<script> var imagee = document.getElementById ("Imagee"), ImageSize = getimagesize (Imagee), ImgWidth = Imagesize[0], ImgHeight = imagesize[1]; Test mode: Console.log (ImgWidth); Console.log (ImgHeight); </script> |
get picture size kb,mb form
| code is as follows |
copy code |
| <input type= "file" onchange= "Checksize ()" id= "F"/> <script Type= "Text/javascript" var ie=!-[1,]; //Distinguishing IE Function checksize () { var f= Document.getelementbyidx_x ("F"); if (IE) { var img=new Image ();//Dynamically create IMG Img.src=f.value; Img.style.display= "None"; Img.onreadystatechange=function () { if (img.readystate== ' complete ') {//When picture load complete Alert ( img.filesize);//ie get file size } } Document.body.removeChlid (IMG);//Get size end, remove picture } Else { var fv=f.files[0].size;//firefox, and other standard value methods Alert (FV); } } </script> |
Based on the above example, let's do a preview of the image before uploading it using the Get Picture size feature.
| The code is as follows |
Copy Code |
| <HTML> <HEAD> <TITLE> demo picture and so on scale shrink </TITLE> <script> function Wa_setimgautosize (IMG) { var img=document.all.img1;//get Pictures var maxwidth=200;//set Picture width limit var maxheight=100;//set Picture height limit var heightwidth=img.offsetheight/img.offsetwidth;//set aspect ratio var widthheight=img.offsetwidth/img.offsetheight;//set ratio of width to height Alert ("Test" +img.offsetheight+img.filesize); if (img.offsetheight>1) alert (img.offsetheight); if (img.readystate!= "complete") { Return false;//ensure the picture is fully loaded } if (img.offsetwidth>maxwidth) { Img.width=maxwidth; Img.height=maxwidth*heightwidth; } if (img.offsetheight>maxheight) { Img.height=maxheight; Img.width=maxheight*widthheight; } } function Checkimg (IMG) { var message= ""; var maxwidth=1;//set Picture width limit var maxheight=1;//set Picture height limit
if (img.readystate!= "complete") { Return false;//ensure the picture is fully loaded } if (img.offsetheight>maxheight) message+= "R height Excess:" +img.offsetheight; if (img.offsetwidth>maxwidth) message+= "R width Excess:" +img.offsetwidth; if (message!= "") alert (message); } </script> </HEAD> <BODY>
<br> <input id=inp type= "file" onpropertychange= "Img1.src=this.value;" > </BODY> </HTML> |