How to get image size in Javascript - get width and height

How can I measure the dimensions of specific image using Javascript ? For example how can I get image width and height using onClick ?
13 Jun 2022 at 06:51 PM
0give a positive ratinggive a negative ratingshow more
LarsLars
Hi,

To get image width and height using Javascript, you can use the following function:


<script>

function imageSize(url) {

var img = new Image();
img.onload = function(){
var imageWidth = img.width;
var imageHeight = img.height;
alert('Width: '+imageWidth+' / Height: '+imageHeight);
}
img.src = url;
}

</script>

<a href="javascript:void(0);" onClick="javascript: imageSize('https://www.example.com/image.png');">Get image size</a>

14 Jun 2022 at 11:09 AM
1give a positive ratinggive a negative ratingshow more
TimTim
Share on FacebookShare on TwitterShare on LinkedInSend email
Follow us on Facebook
2022 AnswerTabsTermsContact us