How to get Image size using Javascript

Last updated on February 14th, 2022 at 01:16 pm

Click to rate this tutorial!
[Total: 1 Average: 5]

Get Image Size Using Javascript

I am using getImgSize() function here which will in turn get the height and width of the image in question.
Image source is defined with an ID demoImg from which we get all the details of the image. Size of the image will be width * height.

<script language="JavaScript">
function getImgSize(imgSrc)
{var newImg = new Image();
newImg.src = imgSrc;
var height = newImg.height;
var width = newImg.width;
alert ('The image size is '+width+'*'+height);
}</script>
<img src="1.jpg" id="demoImg" />
<button onclick="getImgSize(document.getElementById('demoImg').src);" value="Click Me">
</button>
Click to rate this tutorial!
[Total: 1 Average: 5]

Leave a Reply

Your email address will not be published.