How to get the width and height of an image ?

24 Jun, 2019
Given an image and the task is to get the width and height of the image using JavaScript. The width and height property is used to display the image width and height.
Syntax for width:
var width = this.width;
Syntax for height:
var height = this.height;
Example 1: This example selects the image and then use this.width and this.height method to get the width and height of the image.
<!DOCTYPE html><html>      <head>    <title>        Get the real width and height of        an image using JavaScript    </title></head>  <body style="text-align:center;">          <h2 style="color:green;">            GeeksforGeeks        </h2>          <h2 style="color:purple;">            Getting the real width and height of an image        </h2>          <script>            function CheckImageSize() {                var image = document.getElementById("Image").files[0];                createReader(image, function(w, h) {                      alert("Width is: " + w +                     "pixels, Height is: " + h + "pixels");                });            }              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);            }        </script>          <input type="file" id="Image" />        <input type="button" value="Find the dimensions"                            onclick="CheckImageSize()"/></body>  <html>
Output:
Before clicking the button:
After clicking the button:
Example 2: This example display the dimension of an image. It will display the result without using the alert function. Here we will show the result in the same window.
<!DOCTYPE html><html>  <head>    <title>        Get the real width and height of        an image using JavaScript    </title></head>  <body style="text-align:center;">          <img id="myImg" src="https://media.geeksforgeeks.org/wp-content/uploads/20190613121627/download9.png">          <p>        Click the button to display the width        and height of the image    </p>          <button onclick="myFunction()">Try it</button>          <p>The width of the image in pixels:</p>          <p id="geeks"></p>          <p>The height of the image in pixels:</p>          <p id="gfg"></p>          <script>        function myFunction() {            var x = document.getElementById("myImg").width;            var y = document.getElementById("myImg").height;            document.getElementById("geeks").innerHTML = x;            document.getElementById("gfg").innerHTML = y;        }    </script></body>  </html>                    
Output:
Before clicking the button.
After clicking the button:
JavaScript-Misc
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Remove elements from a JavaScript Array
Difference Between PUT and PATCH Request
Roadmap to Learn JavaScript For Beginners
JavaScript | Promises
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?