Last active
May 25, 2019 20:54
-
-
Save tilayealemu/2519f54222ad9a7d7ff2b294b2b32a09 to your computer and use it in GitHub Desktop.
Get largest image in html page using php
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 | |
// example call: http://localhost/largest.php?url_without_http=bbc.co.uk | |
$url = $_GET['url_without_http']; | |
$largest = get_largest_img("http://" . $url); | |
$embed = empty($largest) ? "Suitable image not found" : "<img src='" . $largest . "'>"; | |
echo("<html><body>" . $embed . "</body></html>"); | |
function get_largest_img($url) { | |
libxml_use_internal_errors(true); | |
$html = file_get_contents($url); | |
$dom = new DOMDocument(); | |
$dom->loadHTML($html); | |
$imgs = $dom->getElementsByTagname('img'); | |
$largest = ""; | |
$largest_area = 0; | |
foreach ($imgs as $img) { | |
$img_url = $img->getAttribute("src"); | |
if (empty($img_url)){ | |
continue; | |
} | |
if (substr( $img_url, 0, 2 ) === "//"){ | |
$img_url = "http:" . $img_url; | |
} else if (substr( $img_url, 0, 1 ) === "/"){ | |
$img_url = $url . $img_url; | |
} | |
$size = getimagesize($img_url); | |
if (is_suitable($size)) { | |
$width = $size[0]; | |
$height = $size[1]; | |
if ($width*$height > $largest_area) { | |
$largest = $img_url; | |
$largest_area = $width*$height; | |
} | |
} | |
} | |
return $largest; | |
} | |
function is_suitable($size) { | |
// no image for some reason | |
if (is_null($size)) { | |
return false; | |
} | |
$width = $size[0]; | |
$height = $size[1]; | |
// long images are uninteresting (eg. they can be advert banners) | |
if ($width/$height > 10 || $width/$height < 0.1) { | |
return false; | |
} | |
return true; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment