Get the real width and height of an image with JavaScript? (in Safari/Chrome)

后端 未结 30 2782
傲寒
傲寒 2020-11-22 01:16

I am creating a jQuery plugin.

How do I get the real image width and height with Javascript in Safari?

The following works with Firefox 3, IE7 and Opera 9:

30条回答
  •  眼角桃花
    2020-11-22 01:33

    Check out this repository in github!

    Great Example to check the Width and Height using Javascript

    https://github.com/AzizAK/ImageRealSize

    ---Edited is requested from some comments ..

    Javascript code:

     function CheckImageSize(){
    var image = document.getElementById("Image").files[0];
               createReader(image, function (w, h) {
    
                    alert("Width is: " + w + " And Height is: "+h);
    });            
    }
    
    
      function  createReader(file, whenReady) {
            var reader = new FileReader;
            reader.onload = function (evt) {
                var image = new Image();
                image.onload = function (evt) {
                    var width = this.width;
                    var height = this.height;
                    if (whenReady) whenReady(width, height);
                };
                image.src = evt.target.result;
            };
            reader.readAsDataURL(file);
        }
    

    and HTML code :

    
    
    Image Real Size
    
    
    
    
    
    
    
    

提交回复
热议问题