Is it possible to find the largest Image from the one I get back.
Here is the Code I have so far:
<?php
$url="https://wikipedia.org/wiki/PHP";
$html = file_get_contents($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
echo $tag ->getAttribute('src');
}
?>
For example img 1: 420x120px ; img 2: 1200x300px --> output link Url from img 2
You can use getimagesize()
and max()
in following way:-
$size_array = array(); // create an new empty array
foreach ($tags as $tag) {
$size_array[getimagesize($tag ->getAttribute('src'))] = $tag ->getAttribute('src');
//assign size as key and path as value to the newly created array
}
$max_size = max(array_keys($size_array)); // get max size from keys array
$max_file = $size_array[$max_size]; // find out file path based on max-size
Note:- I have assumed that $tag ->getAttribute('src')
is giving you path of the image files
You may want to create an array containing summations of all width
s and corresponding height
s and desc sort it:
foreach ($tags as $tag) {
$array[] = [
$tag->getAttribute('width') + $tag->getAttribute('height'),
$tag->getAttribute('src')
];
}
array_multisort($array, SORT_DESC);
var_dump($array[0][1]);