php 关于图片获取宽高的优化

需求

1. 应前端需求,在进入文章详情时须要将全部图片进行占位替换,且占位符须要对应图片信息(主要须要知道宽高)
2. 目的:作点击图片浮窗效果

实现方案

1. 优化前
    1.1 正则匹配图片,而后循环获取每张图片的宽高
    1.2 问题:若是文章图片较少,以上操做问题不大。但图片一旦过慢,这个效率将会很是低下
    1.3 代码以下:
preg_match_all('/<img.*? src="(.*?)".*?>/is', $str, $matchs);
       
        if(!empty($matchs[0])){
            $pics = [];
            $i = 0;
            foreach ($matchs[0] as $key => $m) {
                $fileInfo = file_get_contents($matchs[1][$key] . '?x-oss-process=image/info');
                $fileInfo = json_decode($fileInfo, true);
                $data['Width'] = $fileInfo['ImageWidth']['value'];
                $data['Height'] = $fileInfo['ImageHeight']['value'];
                    
                $imgs[$i]['ref'] = '<!--IMG#' . $key . '-->';
                $imgs[$i]['pixel'] = $data['Width'] . '*' . $data['Height'];
                preg_match('/alt="(.*?)"/i', $matchs[0][$key], $mt);
                $imgs[$i]['alt'] = isset($mt[1]) ? $mt[1] : '';   //图片alt
                $imgs[$i]['src'] = $matchs[1][$key];                //图片地址
                $str = str_replace($m, '<!--IMG#' . $key . '-->', $str);
                $i++;

            }
        }
2. 优化思路
    2.1 想着是否会有极速获取图片法子?在网上找了一些资料,基本上都是经过读取图片部分文件信息,不须要下载/读取整个图片。找了一个类库:[https://github.com/tommoor/fastimage](https://github.com/tommoor/fastimage),试了一下。 相比之前的思路(完整的下载图片) 确实有性能上的提高。有兴趣的朋友能够试试,若是针对单张图片的信息获取,这个仍是很推荐的。但批量的实现彷佛还达不到目的
    2.2 分析以上操做,其实慢的过程应该仍是停留在循环获取图片资源上。那么换个思路,我批量获取图片是否就ok了?上代码
preg_match_all('/<img.*? src="(.*?)".*?>/is', $str, $matchs);

if(!empty($matchs[0])){
    //$time = microtime(true);
    //echo  '  ---- start ' . PHP_EOL;

    foreach ($matchs[0] as $key => $m) {
        $urls[] = $matchs[1][$key] . '?x-oss-process=image/info';
    }
    $imageInfos = batchCurl($urls);

    $i = 0;
    foreach ($matchs[0] as $key => $m) {
        $image = json_decode($imageInfos[$key], true);
        $_img['Width'] = $width= $image['ImageWidth']['value'];
        $_img['Height'] = $height = $image['ImageHeight']['value'];

        $imgs[$i]['ref'] = '<!--IMG#' . $key . '-->';
        $imgs[$i]['pixel'] = $_img['Width'] . '*' . $_img['Height'];
        preg_match('/alt="(.*?)"/i', $matchs[0][$key], $mt);
        $imgs[$i]['alt'] = isset($mt[1]) ? $mt[1] : '';   //图片alt
        $imgs[$i]['src'] = $matchs[1][$key];                //图片地址
        $str = str_replace($m, '<!--IMG#' . $key . '-->', $str);

        $i++;
    }
    //echo  " ---- end  px in " . (microtime(true)-$time) . " seconds \n";
    //exit;
}
        
function batchCurl($urls)
{
    $res = $conn = [];

    // 建立批处理cURL句柄
    $mh = curl_multi_init();

    foreach ($urls as $i => $url) {
        // 建立一对cURL资源
        $conn[$i] = curl_init();
        // 设置URL和相应的选项
        curl_setopt($conn[$i], CURLOPT_URL, $url);
        curl_setopt($conn[$i], CURLOPT_HEADER, 0);
        curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($conn[$i], CURLOPT_TIMEOUT, 10);
        // 302跳转
        curl_setopt($conn[$i], CURLOPT_FOLLOWLOCATION, 1);
        // 增长句柄
        curl_multi_add_handle($mh, $conn[$i]);
    }
    $active = null;
    //防卡死写法:执行批处理句柄
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);

    while ($active && $mrc == CURLM_OK) {
        if (curl_multi_select($mh) != -1) {
            do {
                $mrc = curl_multi_exec($mh, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }
    foreach ($urls as $i => $url) {
        //获取当前解析的cURL的相关传输信息
        $info = curl_multi_info_read($mh);
        //获取请求头信息
        $heards = curl_getinfo($conn[$i]);
        //获取输出的文本流
        $res[$i] = curl_multi_getcontent($conn[$i]);
        // 移除curl批处理句柄资源中的某个句柄资源
        curl_multi_remove_handle($mh, $conn[$i]);
        //关闭cURL会话
        curl_close($conn[$i]);
    }
    //关闭所有句柄
    curl_multi_close($mh);

    return $res;
}
3. 测试性能,20张图片的效率几乎能达到秒级
![image](/img/bVcKCF2)

总结

1. 优化的思路很重要,重点在于分析所提所在,对症下药。在此记个笔记