2017-01-28 37 views
1

是否可以從我回來的那個地方找到最大的圖像。從URL獲取最大的圖像via 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'); 

} 
?> 

例如圖1:420x120px;圖2:1200x300px - 從IMG>輸出鏈接URL 2

+1

檢查['getimagesize'](http://php.net/manual/en/function.getimagesize.php#refsect1-function.getimagesize-examples) –

回答

-1

您可以按以下方式使用getimagesize()max(): -

$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')是給你的路徑圖像文件

0

您可能希望創建一個包含所有width S的總和與相應height S和降序排序一個數組:

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