douyi2107 2017-01-28 18:01
浏览 53
已采纳

通过php从URL获取最大的图像

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

  • 写回答

2条回答 默认 最新

  • doujiang2812 2017-01-28 18:09
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器