How to get the actual dimension of image

79 views Asked by At

I am doing a project in which i have to extract the important images from any website. Suppose we have following code :

<img src="/example/example.gif" width="40" height="40" />

From the above code we can easily find the image width and height using Jsoup Library. Now suppose we have following code :

<img src="/example/example.gif"/>

So, Is there any method to find the width and height of image without downloading that file.

1

There are 1 answers

0
Vivin Paliath On

Not without actually rendering the page or downloading the image. Even with width = "40" and height = "40", you are only getting the value of height and width which may not be the dimensions of the actual image.

The best way to do this would be to use something like PhantomJS, which is a headless webkit browser. You then will have to write a script that loads up the page (i.e., "renders" it by building up the DOM in memory) and then you can use the following code to get the actual height and width of the image:

//assuming img is an image element
var width = img.clientWidth;
var height = img.clientHeight;