Skip to content
Related Articles

Related Articles

Improve Article

How to get the width and height of an image ?

  • Difficulty Level : Expert
  • Last Updated : 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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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:
    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.




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

Output:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :