How to get image dimensions with JavaScript?

Spread the love

Sometimes, we want to get image dimensions with JavaScript.

In this article, we’ll look at how to get image dimensions with JavaScript.

How to get image dimensions with JavaScript?

To get image dimensions with JavaScript, we use the width and height properties.

For instance, we write

const img = new Image();

img.onload = () => {
  const { height, width } = img;

  //...
};

img.src = url;

to get the width and height from the img img element.

onload is called after the src property is set to the image url and the image is loaded successfully.

Conclusion

To get image dimensions with JavaScript, we use the width and height properties.

Leave a Reply

Your email address will not be published. Required fields are marked *