Skip to content

Instantly share code, notes, and snippets.

@huned
Created May 7, 2013 19:25
Show Gist options
  • Select an option

  • Save huned/5535404 to your computer and use it in GitHub Desktop.

Select an option

Save huned/5535404 to your computer and use it in GitHub Desktop.
get the biggest image on a page with a reasonable aspect ratio with javascript
var biggestImage = function() {
biggest = null;
images = document.getElementsByTagName('img');
for (var i = 0; i < images.length; i++) {
var image = images[i];
var area = image.width * image.height;
var ratio = image.width / image.height;
if(!biggest || (area > 1 && area > (biggest.width * biggest.height) && ((ratio > 1 && ratio < 2) || (1/ratio > 1 && 1/ratio < 2))))
{
biggest = image;
}
}
return biggest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment