Emily Webb
Emily Webb

Reputation: 335

How to get the display/visual image size (not the actual image size) in JSOUP?

I need to get the display/visual image dimensions of an images in a html page. I am using JSOUP.

I can get the actual image dimensions by retrieving the images as stream but I don't need the actual size of the images instead I need the display size only.

There is no guaranty that whether the img tags contains the height and width html attributes so I cannot rely on getAttribute method.

It would be great help If I can get any suggestions on this.

Thanks in advance.

Upvotes: 1

Views: 1148

Answers (2)

leocl1
leocl1

Reputation: 161

This is a non-trivial task.

To get the true image size that works for any page (including dynamically added content) would be to have a headless browser and query the display size using javascript after the document is fully loaded. If you can inject jQuery javascript framework, then jQuery.width() jQuery.height() will return the rendered width and height respectively. Pure javascript functions that do the same are browser specific.

Assuming that the images are not added or altered dynamically, it would be sufficient to parse to look for:

  1. html width or height attributes set directly on the html tag like so <img width="150px" height="100px">,
  2. css width or height set in the style attribute like so <img style="width: 150px; height: 100px;">, and finally
  3. parse all css files linked into the document and find css rules through the that match the image tags by id and class <img id="some-image-id" class="some-class"> to some css rules that can look like this #some-image-id, .some-image-class { width: 150px; height: 100px; }

Upvotes: 2

superbob
superbob

Reputation: 1638

JSOUP will only give you what is in the HTML, so you must rely on what is in the markup (img tag attributes). The actual size depends on how the browser will display it, it is vendor specific.

Upvotes: 1

Related Questions