How to get the width and height of an image ?

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Before clicking the button:
    ngcut
  • After clicking the button:
    ngcut

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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=
      
    <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>                    

chevron_right


Output:

  • Before clicking the button.
    ngcut
  • After clicking the button:
    ngcut


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.