Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Get image dimensions

I only have a URL to an image. I need to determine the height and width of this image using only JavaScript. The image cannot be visible to the user on the page. How can I get its dimensions?

like image 406
JQueeeer Avatar asked Apr 12 '11 09:04

JQueeeer


People also ask

How do you find the dimensions of a picture?

You can also right-click on an image & choose properties from the drop-down menu. A new window will appear with several tabs. You'll click the details tab, and there you'll find you image size and dimensions.

How do you get the image width and height using JS?

Answer: Use the JavaScript clientWidth property You can simply use the JavaScript clientWidth property to get the current width and height of an image. This property will round the value to an integer.


2 Answers

var img = new Image();  img.onload = function(){   var height = img.height;   var width = img.width;    // code here to use the dimensions }  img.src = url; 
like image 174
Shumii Avatar answered Oct 17 '22 06:10

Shumii


Make a new Image

var img = new Image(); 

Set the src

img.src = your_src 

Get the width and the height

//img.width //img.height 
like image 30
nc3b Avatar answered Oct 17 '22 05:10

nc3b