本篇文章給大家介紹一下php優(yōu)化圖片獲取寬高的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。
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) { //獲取當前解析的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; }
3. 測試性能,20張圖片的效率幾乎能達到秒級

推薦學習:php視頻教程