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

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax clear click file file upload function get html

The use of the picture size and picture sizes (height and width) to note that the picture must be completed before JS get the picture width and height, so to judge in the readystate== "complete" state to get the size, if it is to use file upload,  Always click to clear image=new image (); Imgage.width and Height Oh.
<!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= "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+= "R height Excess:" +img.offsetheight;
if (img.offsetwidth>maxwidth) message+= "R width Excess:" +img.offsetwidth;
if (message!= "") alert (message);
}

</script>
</head>
<body>
<img src= "images/frequency.gif" border=0 id= "IMG1" Onload= "checkimg (this);" >
<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>  
<img src= "test.jpg" mce_src= "test.jpg"/>  
< Script type= "text/web Effects" ><!--  
var a=qford_getimagesize ("Test.jpg");  
Alert (' Width: ' +a.width + '   High: ' +a.height ';
</script>

Related Article

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.