【问题标题】:Making domdocument img search more lightweight让 domdocument img 搜索更轻量级
【发布时间】:2016-05-21 12:53:58
【问题描述】:

这会抓取页面中的所有图像,并且应该检查图像的宽度和高度是否超过 200。如果是,则抓取其中的第一个。但这是一个昂贵的过程,我想知道是否有比使用 getimagesize 更轻量级的方法。有谁知道不使用 YQL 等外部服务的不同方法?

if($ogimage!=''|| !empty($ogimage)){
    $arrimg = $ogimage;
} else {
    $imgarr = array();
foreach ($doc->getElementsByTagName('img') as $img) {
    $arrimg_push = $img->getAttribute('src');
    array_push($imgarr, $arrimg_push);
}
    $i=0;
    foreach($imgarr as $img){
     list($width, $height, $type, $attr) = getimagesize($img);
        if($width > 200 && $height > 200){
            if($i > 0){
            $arrimg = $img;
            $i++;
            }
        }
    }
}

【问题讨论】:

  • 文档中图片的数量是多少?
  • 它会有所不同,它会抓取用户提供的网址。所以它可以是 0 - 1000,具体取决于网站。
  • 这显然是要花费很长时间来执行代码,因为目标图像在远程服务器中!
  • 好点!也许我只是在此期间做一个花哨的加载动画或其他东西。

标签: php domdocument getimagesize


【解决方案1】:

ImageMagick 的pingImage or pingImageFile 将尽可能少地读取图像文件以获得基本属性,然后可以使用getImageWidthgetImageHeight 访问这些属性。

【讨论】:

    猜你喜欢
    • 2010-09-10
    • 2010-09-07
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 2011-08-26
    • 2022-01-08
    • 2013-12-12
    • 2014-07-29
    相关资源
    最近更新 更多