JavaScript Get Image Height and Width Examples

In JavaScript, you can use the .naturalWidth and .naturalHeight properties of the HTMLImageElement object to get the original width and height of an image, respectively. These properties return the dimensions of the image file itself, not the dimensions it is displayed at on the webpage. Here's an example of how to use them:


var img = document.getElementById("my-image");
var width = img.naturalWidth;
var height = img.naturalHeight;
console.log(width + " x " + height);

Alternatively, you can use the .width and .height properties of the HTMLImageElement object to get the dimensions of the image as it is displayed on the webpage. These properties return the dimensions of the image as it is currently rendered on the page, which may be different from the original dimensions of the image file.


var img = document.getElementById("my-image");
var width = img.width;
var height = img.height;
console.log(width + " x " + height);

Note that the .naturalWidth and .naturalHeight properties are not supported in some older browsers.

Jquery Get Image Height and Width

In jQuery, you can use the .width() and .height() methods to get the width and height of an image, respectively. These methods return the dimensions of the image as it is currently displayed on the webpage. Here's an example of how to use them:


var $img = $("#my-image");
var width = $img.width();
var height = $img.height();
console.log(width + " x " + height);

Alternatively, you can use the .prop('naturalWidth') and .prop('naturalHeight') to get the original width and height of an image, respectively. These properties return the dimensions of the image file itself, not the dimensions it is displayed at on the webpage.


var $img = $("#my-image");
var width = $img.prop('naturalWidth');
var height = $img.prop('naturalHeight');
console.log(width + " x " + height);

It's important to note that the .width() and .height() methods will return the dimensions of the image as it is currently rendered on the page, which may be different from the original dimensions of the image file. The .prop('naturalWidth') and .prop('naturalHeight') methods will return the actual dimensions of the image file.

React Js Get Image Height and Width

In ReactJS, you can use the useRef hook and the .current.naturalWidth and .current.naturalHeight properties of the HTMLImageElement object to get the original width and height of an image, respectively. These properties return the dimensions of the image file itself, not the dimensions it is displayed at on the webpage.

Here's an example of how to use it:


import { useRef } from 'react';
function MyComponent() {
  const imgRef = useRef(null);
  const handleClick = () => {
    const width = imgRef.current.naturalWidth;
    const height = imgRef.current.naturalHeight;
    console.log(`${width} x ${height}`);
  }
  return (
    <div>
      <img ref={imgRef} src="path/to/image.jpg" onLoad={handleClick} />
    </div>
  );
}

Note that the .naturalWidth and .naturalHeight properties are not supported in some older browsers. You can also use the state and setState Hooks to store the width and height of an image after it has been loaded.


import { useState } from 'react';
function MyComponent() {
  const [imgWidth, setImgWidth] = useState(0);
  const [imgHeight, setImgHeight] = useState(0);
  const handleImageLoad = e => {
    setImgWidth(e.target.naturalWidth);
    setImgHeight(e.target.naturalHeight);
  }
  return (
    <div>
      <img src="path/to/image.jpg" onLoad={handleImageLoad} />
      <p>{`${imgWidth} x ${imgHeight}`}</p>
    </div>
  );
}

In this example, the handleImageLoad function is passed as the onLoad event handler for the img element, so it will be invoked when the image is finished loading.

Angular Js Get Image Height and Width

In AngularJS, you can use the $element object to access the underlying DOM element of an image, and then use the .naturalWidth and .naturalHeight properties of the HTMLImageElement object to get the original width and height of the image, respectively. These properties return the dimensions of the image file itself, not the dimensions it is displayed at on the webpage. Here's an example of how to use it in a directive:


app.directive('myImage', function() {
  return {
    restrict: 'E',
    link: function(scope, $element) {
      var img = $element[0];
      var width = img.naturalWidth;
      var height = img.naturalHeight;
      console.log(width + " x " + height);
    }
  };
});

You can also use the $timeout service to wait until the image is loaded, and then get its dimensions.


app.directive('myImage', function($timeout) {
  return {
    restrict: 'E',
    link: function(scope, $element) {
      var img = $element[0];
      $timeout(function() {
        var width = img.naturalWidth;
        var height = img.naturalHeight;
        console.log(width + " x " + height);
      }, 0);
    }
  };
});

Note that the .naturalWidth and .naturalHeight properties are not supported in some older browsers.

It's important to keep in mind that these examples are for directives, but you can also use $element service to get the image element inside a controller and use the same approach as well.