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

Recommended Posts:
- How to get the image size (height & width) using JavaScript ?
- CSS | Height and Width
- How to animate div width and height on mouse hover using jQuery ?
- CSS | border-image-width Property
- HTML | DOM Input Image width Property
- HTML | DOM input image height Property
- Convert an image into grayscale image using HTML/CSS
- jQuery | width()
- CSS | max-width Property
- CSS | min-width Property
- p5.js | width variable
- HTML | DOM HR width Property
- CSS | outline-width Property
- CSS | border-top-width Property
- How to get the width of scroll bar using JavaScript ?
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.



