How to get image size (height & width) using JavaScript?

前端 未结 29 3087
忘了有多久
忘了有多久 2020-11-21 05:23

Are there any JavaScript or jQuery APIs or methods to get the dimensions of an image on the page?

相关标签:
29条回答
  • 2020-11-21 06:02

    ok guys, i think i improved the source code to be able to let the image load before trying to find out its properties, otherwise it will display '0 * 0', because the next statement would have been called before the file was loaded into the browser. Requires jquery...

    function getImgSize(imgSrc){
        var newImg = new Image();
        newImg.src = imgSrc;
        var height = newImg.height;
        var width = newImg.width;
        p = $(newImg).ready(function(){
            return {width: newImg.width, height: newImg.height};
        });
        alert (p[0]['width']+" "+p[0]['height']);
    }
    
    0 讨论(0)
  • 2020-11-21 06:05

    You can only really do this using a callback of the load event as the size of the image is not known until it has actually finished loading. Something like the code below...

    var imgTesting = new Image();
    
    function CreateDelegate(contextObject, delegateMethod)
    {
        return function()
        {
            return delegateMethod.apply(contextObject, arguments);
        }
    }
    
    function imgTesting_onload()
    {
        alert(this.width + " by " + this.height);
    }
    
    
    imgTesting.onload = CreateDelegate(imgTesting, imgTesting_onload);
    imgTesting.src = 'yourimage.jpg';
    
    0 讨论(0)
  • 2020-11-21 06:05

    Before using real image size you should load source image. If you use JQuery framework you can get real image size in simple way.

    $("ImageID").load(function(){
      console.log($(this).width() + "x" + $(this).height())
    })
    
    0 讨论(0)
  • 2020-11-21 06:08

    If you are using jQuery and you are requesting image sizes you have to wait until they load or you will only get zeroes.

    $(document).ready(function() {
        $("img").load(function() {
            alert($(this).height());
            alert($(this).width());
        });
    });
    
    0 讨论(0)
  • 2020-11-21 06:08

    I think an update to these answers is useful because one of the best-voted replies suggests using clientWidth and clientHeight, which I think is now obsolete.

    I have done some experiments with HTML5, to see which values actually get returned.

    First of all, I used a program called Dash to get an overview of the image API. It states that height and width are the rendered height/width of the image and that naturalHeight and naturalWidth are the intrinsic height/width of the image (and are HTML5 only).

    I used an image of a beautiful butterfly, from a file with height 300 and width 400. And this Javascript:

    var img = document.getElementById("img1");
    
    console.log(img.height,           img.width);
    console.log(img.naturalHeight,    img.naturalWidth);
    console.log($("#img1").height(),  $("#img1").width());
    

    Then I used this HTML, with inline CSS for the height and width.

    <img style="height:120px;width:150px;" id="img1" src="img/Butterfly.jpg" />
    

    Results:

    /*Image Element*/ height == 300         width == 400
               naturalHeight == 300  naturalWidth == 400
    /*Jquery*/      height() == 120       width() == 150
    
    /*Actual Rendered size*/    120                  150
    

    I then changed the HTML to the following:

    <img height="90" width="115" id="img1" src="img/Butterfly.jpg" />
    

    i.e. using height and width attributes rather than inline styles

    Results:

    /*Image Element*/ height ==  90         width == 115
               naturalHeight == 300  naturalWidth == 400
    /*Jquery*/      height() ==  90       width() == 115
    
    /*Actual Rendered size*/     90                  115
    

    I then changed the HTML to the following:

    <img height="90" width="115" style="height:120px;width:150px;" id="img1" src="img/Butterfly.jpg" />
    

    i.e. using both attributes and CSS, to see which takes precedence.

    Results:

    /*Image Element*/ height ==  90         width == 115
               naturalHeight == 300  naturalWidth == 400
    /*Jquery*/      height() == 120       width() == 150
    
    /*Actual Rendered size*/    120                  150
    
    0 讨论(0)
  • 2020-11-21 06:08

    You can apply the onload handler property when the page loads in js or jquery like this:-

    $(document).ready(function(){
       var width = img.clientWidth;
       var height = img.clientHeight;
    
     });
    
    0 讨论(0)
提交回复
热议问题