JavaScript(JS) JS get the dimensions of an image

www.ig‮ditfi‬ea.com

In JavaScript, you can get the dimensions of an image using the Image object. Here's an example:

const image = new Image();

image.src = 'image.png';

image.onload = function() {
  const width = this.width;
  const height = this.height;
  console.log(`The image dimensions are ${width} x ${height}`);
};

In the above example, we create a new Image object and set its src property to the URL of the image file. We then define an onload event handler for the Image object that gets called when the image has finished loading. Inside the event handler, we use the width and height properties of the Image object to get the dimensions of the image. We then log the dimensions to the console.

Note that you should only attempt to get the dimensions of an image after it has finished loading. If you try to get the dimensions of an image before it has finished loading, the width and height properties may not be accurate.