php从url获取所有图像,其宽度和高度>=再快200

php从url获取所有图像,其宽度和高度>=再快200,php,getimagesize,Php,Getimagesize,我模拟了一些功能,如添加pin 如何更快地从宽度和高度>=200的url获取所有图像pinterest.com将完成整个过程近10秒,但我需要48.64秒 require dirname(__FILE__) . '/simple_html_dom.php'; $url = 'http://www.huffingtonpost.com/'; $html = file_get_html($url); if($html->find('img')){ foreach($html->f

我模拟了一些功能,如
添加pin

如何更快地从宽度和高度>=200的url获取所有图像
pinterest.com
将完成整个过程近10秒,但我需要48.64秒

require dirname(__FILE__) . '/simple_html_dom.php';
$url = 'http://www.huffingtonpost.com/';
$html = file_get_html($url);
if($html->find('img')){
    foreach($html->find('img') as $element) {
        $size = @getimagesize($element->src);
        if($size[0]>=200&&$size[1]>=200){
            echo $element;
        }
    }
}// cost 48.64 seconds
getimagesize()将首先下载整个图像文件,然后进行分析。通常,您只需要文件的前几百个字节就可以获得类型/解析详细信息。另外,它将对每个图像使用单独的http请求


一个经过适当优化的系统将使用部分get请求只获取图像的第一块,并利用http keep alives将TCP连接开销保持在最小值。

那么从html读取宽度和高度呢?我知道有些图像可能没有这个属性,但也许你可以跳过这个属性小于200px的图像


这只是一个想法,但可能不适用于你

我认为您使用的方法是使用
curl\u multi\u init运行
curl
中的
parallel
请求。有关更多信息,请参阅。通过这种方式,它可以更快地加载,避免所有可能影响速度的带宽问题

将映像保存到本地临时目录中,不要直接在本地运行
getimagesize()
,这比通过
http://

我希望这有帮助

编辑1

注***

A.并非所有图像都以
http

并非所有图像都有效

C.创建需要存储图像的
temp
文件夹

概念证明

require 'simple_html_dom.php';
$url = 'http://www.huffingtonpost.com';
$html = file_get_html ( $url );
$nodes = array ();
$start = microtime ();
$res = array ();

if ($html->find ( 'img' )) {
    foreach ( $html->find ( 'img' ) as $element ) {
        if (startsWith ( $element->src, "/" )) {
            $element->src = $url . $element->src;
        }
        if (! startsWith ( $element->src, "http" )) {
            $element->src = $url . "/" . $element->src;
        }
        $nodes [] = $element->src;
    }
}

echo "<pre>";
print_r ( imageDownload ( $nodes, 200, 200 ) );
echo "<h1>", microtime () - $start, "</h1>";

function imageDownload($nodes, $maxHeight = 0, $maxWidth = 0) {

    $mh = curl_multi_init ();
    $curl_array = array ();
    foreach ( $nodes as $i => $url ) {
        $curl_array [$i] = curl_init ( $url );
        curl_setopt ( $curl_array [$i], CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $curl_array [$i], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)' );
        curl_setopt ( $curl_array [$i], CURLOPT_CONNECTTIMEOUT, 5 );
        curl_setopt ( $curl_array [$i], CURLOPT_TIMEOUT, 15 );
        curl_multi_add_handle ( $mh, $curl_array [$i] );
    }
    $running = NULL;
    do {
        usleep ( 10000 );
        curl_multi_exec ( $mh, $running );
    } while ( $running > 0 );

    $res = array ();
    foreach ( $nodes as $i => $url ) {
        $curlErrorCode = curl_errno ( $curl_array [$i] );

        if ($curlErrorCode === 0) {
            $info = curl_getinfo ( $curl_array [$i] );
            $ext = getExtention ( $info ['content_type'] );
            if ($info ['content_type'] !== null) {
                $temp = "temp/img" . md5 ( mt_rand () ) . $ext;
                touch ( $temp );
                $imageContent = curl_multi_getcontent ( $curl_array [$i] );
                file_put_contents ( $temp, $imageContent );
                if ($maxHeight == 0 || $maxWidth == 0) {
                    $res [] = $temp;
                } else {
                    $size = getimagesize ( $temp );
                    if ($size [1] >= $maxHeight && $size [0] >= $maxWidth) {
                        $res [] = $temp;
                    } else {
                        unlink ( $temp );
                    }
                }
            }
        }
        curl_multi_remove_handle ( $mh, $curl_array [$i] );
        curl_close ( $curl_array [$i] );

    }

    curl_multi_close ( $mh );
    return $res;
}

function getExtention($type) {
    $type = strtolower ( $type );
    switch ($type) {
        case "image/gif" :
            return ".gif";
            break;
        case "image/png" :
            return ".png";
            break;

        case "image/jpeg" :
            return ".jpg";
            break;

        default :
            return ".img";
            break;
    }
}

function startsWith($str, $prefix) {
    $temp = substr ( $str, 0, strlen ( $prefix ) );
    $temp = strtolower ( $temp );
    $prefix = strtolower ( $prefix );
    return ($temp == $prefix);
}
谢谢 :)

使用
imagecreatefromstring
imagesx
imagesy
,此操作应在30秒内运行。比getimagesize()快一点


这里定义了部分GET:基本上只是一个普通请求,但有一个
范围:
头来指定要传输的字节。您可以使用curl来执行持久http请求:刚刚得到一个在7秒内运行的脚本——仍然在测试这是一个很好的方法,非常感谢。一些问题:如何获取原始图像的url,而不是本地临时文件夹中的输出?这很容易替换
$res[]=$temp
$res[]=$url这将完成任务。。别忘了也要
取消链接
everything@Baba如果($size[0]>=$maxHeight&&$size[0]>=$maxWidth),请修改此条件
。您可能需要将$size[0]更改为$size[1]以进行$maxHeight比较
Array
(
    [0] => temp/img8cdd64d686ee6b925e8706fa35968da4.gif
    [1] => temp/img5811155f8862cd0c3e2746881df9cd9f.gif
    [2] => temp/imga597bf04873859a69373804dc2e2c27e.jpg
    [3] => temp/img0914451e7e5a6f4c883ad7845569029e.jpg
    [4] => temp/imgb1c8c4fa88d0847c99c6f4aa17a0a457.jpg
    [5] => temp/img36e5da68a30df7934a26911f65230819.jpg
    [6] => temp/img068c1aa705296b38f2ec689e5b3172b9.png
    [7] => temp/imgfbeca2410b9a9fb5c08ef88dacd46895.png
)
0.076347
function ranger($url){
    $headers = array( "Range: bytes=0-32768" );
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($curl);
    curl_close($curl);
}
require dirname(__FILE__) . '/simple_html_dom.php';
$url = 'http://www.huffingtonpost.com/';
$html = file_get_html($url);
if($html->find('img')){
    foreach($html->find('img') as $element) {
        $raw = ranger($element->src);
        $im = @imagecreatefromstring($raw);
        $width = @imagesx($im);
        $height = @imagesy($im);
        if($width>=200&&$height>=200){
            echo $element;
        }
    }
}