Use JS to get picture size and picture sizes (height and width)

Source: Internet
Author: User

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>js get picture sizes and picture sizes (height and width) </title>
<script>
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+= "Height excess:" +img.offsetheight;
if (img.offsetwidth>maxwidth) message+= "width excess:" +img.offsetwidth;
if (message!= "") alert (message);
}

</script>
<body>

<br> <input id=inp type= "file" onpropertychange= "Img1.src=this.value;" >
</body>
When the picture is loaded, you can get the picture size successfully, so you want to determine the size of the readystate== "complete"

Test Code Two
<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 get picture dimension function test:</br>  
  
< Script type= "text/web Effects" ><!--  
var a=qford_getimagesize ("Test.jpg");  
Alert (' Width: ' +a.width + '   High: ' +a.height ';
</script> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.