溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php優(yōu)化圖片獲取寬高的方法

發(fā)布時間:2021-06-18 17:35:29 來源:億速云 閱讀:286 作者:chen 欄目:編程語言

本篇內(nèi)容介紹了“php優(yōu)化圖片獲取寬高的方法”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

php 關(guān)于圖片獲取寬高的優(yōu)化

需求

應(yīng)前端需求,在進入文章詳情時需要將所有圖片進行占位替換,且占位符需要對應(yīng)圖片信息(主要需要知道寬高)

目的:做點擊圖片浮窗效果

實現(xiàn)方案

優(yōu)化前

正則匹配圖片,然后循環(huán)獲取每張圖片的寬高

問題:如果文章圖片較少,以上操作問題不大。但圖片一旦過多,這個效率將會非常低下

代碼如下:

        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++;

            }
        }

優(yōu)化思路

想著是否會有極速獲取圖片法子?在網(wǎng)上找了一些資料,基本上都是通過讀取圖片部分文件信息,不需要下載/讀取整個圖片。找了一個類庫:[https://github.com/tommoor/fastimage](https://github.com/tommoor/fastimage),試了一下。 相比以前的思路(完整的下載圖片) 確實有性能上的提升。有興趣的朋友可以試試,如果針對單張圖片的信息獲取,這個還是很推薦的。但批量的實現(xiàn)似乎還達不到目的

分析以上操作,其實慢的過程應(yīng)該還是停留在循環(huán)獲取圖片資源上。那么換個思路,我批量獲取圖片是否就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 = [];

    // 創(chuàng)建批處理cURL句柄
    $mh = curl_multi_init();

    foreach ($urls as $i => $url) {
        // 創(chuàng)建一對cURL資源
        $conn[$i] = curl_init();
        // 設(shè)置URL和相應(yīng)的選項
        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跳轉(zhuǎn)
        curl_setopt($conn[$i], CURLOPT_FOLLOWLOCATION, 1);
        // 增加句柄
        curl_multi_add_handle($mh, $conn[$i]);
    }
    $active = null;
    //防卡死寫法:執(zhí)行批處理句柄
    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) {
        //獲取當(dāng)前解析的cURL的相關(guān)傳輸信息
        $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]);
        //關(guān)閉cURL會話
        curl_close($conn[$i]);
    }
    //關(guān)閉全部句柄
    curl_multi_close($mh);

    return $res;
}

“php優(yōu)化圖片獲取寬高的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI