Categories
JavaScript Answers

How to determine image file size and dimensions via JavaScript?

Spread the love

Sometimes, we want to determine image file size and dimensions via JavaScript.

In this article, we’ll look at how to determine image file size and dimensions via JavaScript.

How to determine image file size and dimensions via JavaScript?

To determine image file size and dimensions via JavaScript, we use the clientWidth and clientHeight properties.

For instance, we write

const img = document.getElementById("imageId");

const width = img.clientWidth;
const height = img.clientHeight;

to select the img element with getElementById.

We get the width with the clientWidth property and the height with the clientHeight property.

Conclusion

To determine image file size and dimensions via JavaScript, we use the clientWidth and clientHeight properties.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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