Open In App

How to get the Width and Height of an Image using JavaScript?

Last Updated : 08 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

let width = this.width;

Syntax for height:

let height = this.height;

Example 1: This example selects the image and then uses this.width and this.height method to get the width and height of the image.

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

    <input type="file" id="Image" />
    <input type="button" value="Find the dimensions"
        onclick="CheckImageSize()" />

    <script>
        function CheckImageSize() {
            let image = document.getElementById("Image").files[0];
            createReader(image, function (w, h) {

                alert("Width is: " + w +
                    "pixels, Height is: " + h + "pixels");
            });
        }

        function createReader(file, whenReady) {
            let reader = new FileReader;
            reader.onload = function (evt) {
                let image = new Image();
                image.onload = function (evt) {
                    let width = this.width;
                    let height = this.height;
                    if (whenReady) whenReady(width, height);
                };
                image.src = evt.target.result;
            };
            reader.readAsDataURL(file);
        }
    </script>


</body>

<html>

Output:

output

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.

html
<!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() {
            let x = document.getElementById("myImg").width;
            let y = document.getElementById("myImg").height;
            document.getElementById("geeks").innerHTML = x;
            document.getElementById("gfg").innerHTML = y;
        }
    </script>
</body>

</html>

Output:

output


Next Article

Similar Reads