Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu December 25, 2022 › #HowTo #image

JavaScript program to get the dimensions of an image

Today, we are going to write a JavaScript program to get the dimensions of an image.

This is very easy. Before we start to write the program let's see the example first.

Example : get image dimensions from URL. #

<script> 
  const img = new Image();
  img.src = 'https://app.3schools.in/img/bg.png';
  img.onload = function() {
    console.log('width ' + this.width)
    console.log('height '+ this.height);
  }
</script>

The Image() constructor creates a new HTMLImageElement instance.

The src property is used to set the image URL.

And finally the onload() method is used to access the height and width of the image (when the image is completely loaded).

save
listen
AI Answer
Write Your Answer
loading
back
View All