JS Get picture Size Code Summary

Source: Internet
Author: User

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>

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.