How to get current image size (width & height) in javascript?

How to get current image size (width & height) in javascript?

Asked on October 30, 2018 in Javascript.
Add Comment


  • 3 Answer(s)

      In js,the clientwidth  property  is used to get the current image size.This property will round the value to an integer.

    For ex:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>JavaScript Find Current Image Width and Height</title>
    <script type="text/javascript">
      function imgSize()
    {
        var myImg = document.querySelector("#sky");
        var currWidth = myImg.clientWidth;
        var currHeight = myImg.clientHeight;
        alert("Current width=" + currWidth + ", " + "Original height=" + currHeight);
      }
    </script>
    </head>
    <body>
      <img src="sky.jpg" id="sky" width="250" alt="Cloudy Sky" contextmenu="skymenu">
      <p><button type="button" onclick="imgSize();">Get Current Image Size</button></p>
    </body>
    </html>
    
    Answered on October 30, 2018.
    Add Comment

    var img = new Image();
    img
    .onload = function() {
    alert
    (this.width + ‘x’ + this.height);
    }
    img
    .src = ‘http://www.google.com/intl/en_ALL/images/logo.gif’;

    Answered on February 24, 2019.
    Add Comment

    var img = new Image();

    img.onload = function(){
    var height = img.height;
    var width = img.width;

    // code here to use the dimensions
    }

    img.src = url;

    Answered on February 24, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.