Created
October 20, 2012 23:42
-
-
Save Lammerink/3925263 to your computer and use it in GitHub Desktop.
PHP : Get Biggest image from URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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