How to get image dimensions from URL (JavaScript)

Let's consider an example of how you can get an image dimensions, knowing its URL.

We create an Image object, then assign a link to it and then after image load we can find out the width and height of the object.

An example:

const img = new Image();
img.src = 'http://mysite.com/myimage.gif'; // change to your URL
img.onload = function() {
console.log(this.width);
console.log(this.height);
}

This example should print to the console the width and height of the image you have specified in the URL.

Comments

No comments yet, you can leave yours: