Skip to content

Instantly share code, notes, and snippets.

@Lammerink
Created October 20, 2012 23:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lammerink/3925263 to your computer and use it in GitHub Desktop.
Save Lammerink/3925263 to your computer and use it in GitHub Desktop.
PHP : Get Biggest image from URL
<?php
// Don't forget to install URL_to_Absolute
// Found at http://www.electrictoolbox.com/examples/url_to_absolute.zip
function get_biggest_img($url){
require_once('simple_html_dom.php'); // PHP Simple HTML DOM Parser.
require_once('url_to_absolute/url_to_absolute.php'); // get image absolute url.
// options
$biggestImage = 'path to "no image found" image'; // Is returned when no images are found.
// process
$maxSize = -1;
$visited = array();
$html = file_get_html($url);
// loop images
foreach($html->find('img') as $element) {
$src = $element->src;
if($src=='')continue;// it happens on your test url
$imageurl = url_to_absolute($url, $src);//get image absolute url
// ignore already seen images, add new images
if(in_array($imageurl, $visited))continue;
$visited[]=$imageurl;
// get image
$image=@getimagesize($imageurl);// get the rest images width and height
if (($image[0] * $image[1]) > $maxSize) {
$maxSize = $image[0] * $image[1]; //compare sizes
$biggest_img = $imageurl;
}
}
return $biggest_img; //return the biggest found image
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment