2017-01-28 12 views
1

私が戻った画像から最大の画像を見つけることは可能でしょうか?たとえば、IMG 1の場合URLからphp経由で最大の画像を取得

<?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'); 

} 
?> 

ここで私がこれまで持っているコードです420x120px。 IMG 2:1200x300px - あなたが道を以下にgetimagesize()max()を使用することができます。IMG 2

+1

['getimagesize'](http://php.net/manual/en/function.getimagesize.php#refsect1-function.getimagesize-examples)を確認してください。 –

答えて

-1

から>出力リンクURL: -

$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 

注: - 私は$tag ->getAttribute('src')はあなたの道を与えていることを想定しています画像はあなたがすべてのwidth Sの和を含むとheight sおよび降順の並べ替え、それに対応する配列を作成することもできます

0

ファイル:

foreach ($tags as $tag) { 
    $array[] = [ 
     $tag->getAttribute('width') + $tag->getAttribute('height'), 
     $tag->getAttribute('src') 
    ]; 
} 
array_multisort($array, SORT_DESC); 
var_dump($array[0][1]); 
関連する問題