最快的速度擷取網頁所有圖片的長和寬。
不知道大家有沒有玩過 http://pinterest.com ?註冊後,它有一個 add a pin, 當你提交一個網站的URL後,按Find Images時,它可以尋找你提交網頁上所有圖片的(並進行長和寬條件的篩選),整個過程一般在10秒左右。
最近想模仿它,做一個小功能組件。已經摒棄掉萬惡的 getimagesize() (需要48.64秒),換用 imagecreatefromstring()(還是需要26.13秒),和它10秒左右的成績,簡直是天壤之別。
要考慮 TCP 串連數,要做到伺服器資源最省化,還要考慮執行時間最少化。求助萬能的大蝦們,如何繼續最佳化代碼?可以跑的更快些。
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);
}//curl設定
require dirname(__FILE__) . '/simple_html_dom.php';
//採用simple_html_dom.php分析HTML nod
$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;//得出長大於大於200,寬大於等於200的圖片
}
}
}
------解決思路----------------------
也許能走個彎路,減輕伺服器網路壓力。
伺服器負責解析HTML資料,統計image標籤資訊,最後將收集的文本資料送回用戶端。
載入圖片由用戶端來完成,只需讀取width,height屬性,就完全可以擷取圖片的原始大小。
好處多多,不過可能的麻煩是防盜鏈
------解決思路----------------------
頂樓上
PHP擷取資源
javascript 取圖片長和寬
------解決思路----------------------
讀取並解析 2.8秒
讀取圖片(138個) 27秒
找到 7 個
僅從最佳化代碼出發,應該油水不大
可考慮多路並發
------解決思路----------------------
讀取並解析 3.6秒
啟動讀取圖片進程(138個) 1.3秒
結果檔案中記錄數 7 個
http://s.huffpost.com/images/v/logos/v4/tagline.gif
http://s.huffpost.com/images/v/logos/v4/homepage.gif?v9
http://i.huffpost.com/gen/559399/thumbs/r-OLBERMANN-huge.jpg
http://s.huffpost.com/images/facebook_promo_connect.png?3
http://images.huffingtonpost.com/2012-04-04-michaeljfoxmarlo2SECOND.jpg
http://images.huffingtonpost.com/2012-04-05-Screenshot20120405at9.40.24AM.jpg
http://i.huffpost.com/gen/557914/thumbs/s-SCORSESE-large300.jpg
原迴圈改為
foreach($html->find('img') as $element) {
tenor("tenorcall.php?v=$element->src");
}
}
tenorcall.php
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);
}//curl設定
$raw = ranger($_GET['v']);
$im = @imagecreatefromstring($raw);
$width = @imagesx($im);
$height = @imagesy($im);
if($width>=200
------解決思路----------------------
$height>=200){
file_put_contents('tenorcall.txt', $_GET['v'].PHP_EOL, FILE_APPEND );//得出長大於大於200,寬大於等於200的圖片
}
/**
* 函數 tenor
* 功能 啟動一個url,但不等待返回
* 參數 $page,待執行的頁面程式
* 返回 無
**/
if(! function_exists('tenor')):
function tenor($page) {
$host = $_SERVER["HTTP_HOST"];
$fp = fsockopen($host, 80, $errno, $errmsg);
if(!$fp) {
echo "$errstr ($errno)
\n";
} else {
fputs($fp,"GET /$page HTTP/1.0\nHost: $host\n\n");
fclose($fp);
}
}
endif;
代碼還是原代碼,非但沒減少,反而增加了
但因為是並發,所以速度明顯提高
值得注意的是:tenor 函數在某些web伺服器中不能穩定的運行(比如iis6)原因不明
------解決思路----------------------
我覺得,讓用戶端載入的方案是可行的,
用戶端再將符合要求的圖片資訊提交給伺服器,伺服器端再驗證一次後儲存。。。
另外32768是怎麼得來的?1-200不夠嗎
------解決思路----------------------
學習! 是用PHP擷取圖片url後直接讀取圖片的頭資訊嗎?
------解決思路----------------------
pinterest那個pin功能創意很好,而且技術很簡單,就是書籤一串js代碼,然後你點這個書籤就相當於往當前頁面文檔append入一個js檔案,這個js檔案怎麼寫,就很簡單了,主要就是遍曆document.getElementsByTagName('img')
------解決思路----------------------
本帖最後由 xuzuning 於 2012-04-06 15:25:06 編輯
138個照片並發,是不是就消耗了138個串連數
對
是否需要修改php.ini,增加串連數
否,串連是向外的,如果要改,也是對方改
CPU和記憶體開銷如何
這個不太好測試
又,關於使用 js 判斷的問題,由於他們沒有給出代碼,無法測試
自己寫了兩個方案都不理想,也就作罷了
用JS並發和直接PHP並發,2者從資源消耗角度來比,哪個會更少
資源消耗角度來比 都一樣,都要完整的載入圖片
不過前者是消耗用戶端資源,後者是消耗伺服器端資源
另外瀏覽器的機制不很瞭解,是否真的是並發也未可知
------解決思路----------------------
這段代碼在我這裡大約 1.8秒,不計算 file_get_html ( $url ) 時間
$res [] = $url ;//$temp;
這樣就是網路地址了
他是儲存為本地檔案後用 getimagesize 擷取尺寸的
他應該是通過 curl 並發的,這個機制我不太瞭解
------解決思路----------------------
但是 if(in_array($absUrl, $visited))continue; 這行報錯。 Warning: in_array() expects parameter 2 to be array, null。
他的代碼中並沒有你說的出錯的代碼
應該是 file_get_html 在報錯吧
file_get_html 使用 file_get_contents 讀取 url 成功率較低
經常要刷兩三次才可獨到資料
------解決思路----------------------
JS可以通過擷取圖片的頭部資訊,而直接擷取到圖片的高度,
這種方式比用圖片載入完成以後在擷取他的搞定效率至少快10倍以上,
之前記得有在一個播客裡面看到過這麼個文章來著,
沒收藏,這一時半會的找不到了,鬱悶啊~
------解決思路----------------------
剛註冊了http://pinterest.com。 它的做法就是用用戶端來載入
點擊Add 選擇Pin ,貼上網址 http://www.huffingtonpost.com/
在chrome的Network中可以看到有一個請求
GET /pin/create/find_images/?url=http%253A%2F%2Fwww.huffingtonpost.com HTTP/1.1
返回的資訊是一個json對象:
images: [http://s.huffpost.com/images/v/logos/v4/homepage.gif?v9,…]
0: "http://s.huffpost.com/images/v/logos/v4/homepage.gif?v9"
1: "http://s.huffpost.com/images/v/logos/v4/tagline.gif"
2: "http://s.huffpost.com/images/splash/t_mini-a.png"
3: "http://s.huffpost.com/images/splash/t_mini-a.png"
4: "http://s.huffpost.com/images/splash/t_mini-a.png"
5: "http://s.huffpost.com/images/splash/t_mini-a.png"
6: "http://s.huffpost.com/images/splash/t_mini-a.png"
7: "http://s.huffpost.com/images/splash/t_mini-a.png"
8: "http://s.huffpost.com/images/splash/t_mini-a.png"
9: "http://s.huffpost.com/images/splash/t_mini-a.png"
10: "http://s.huffpost.com/images/splash/t_mini-a.png"
11: "http://s.huffpost.com/images/splash/t_mini-a.png"
12: "http://s.huffpost.com/images/splash/t_mini-a.png"
13: "http://s.huffpost.com/images/splash/t_mini-a.png"
14: "http://s.huffpost.com/images/splash/t_mini-a.png"
15: "http://s.huffpost.com/images/splash/t_mini-a.png"
16: "http://s.huffpost.com/images/splash/t_mini-a.png"
17: "http://i.huffpost.com/gen/560770/thumbs/r-GSA-LAS-VEGAS-VIDEO-huge.jpg"
18: "http://s.huffpost.com/images/webslice12x12.png"
19: "http://s.huffpost.com/images/v/blog_column.png"
20: "http://s.huffpost.com/contributors/gary-hart/headshot.jpg"
21: "http://www.huffingtonpost.com/images/trans.gif"
22: "http://www.huffingtonpost.com/images/trans.gif"
23: "http://www.huffingtonpost.com/images/trans.gif"
24: "http://images.huffingtonpost.com/2012-04-06-campbellguitar.jpg"
25: "http://www.huffingtonpost.com/images/trans.gif"
26: "http://www.huffingtonpost.com/images/trans.gif"
27: "http://www.huffingtonpost.com/images/trans.gif"
28: "http://www.huffingtonpost.com/images/trans.gif"
29: "http://www.huffingtonpost.com/images/trans.gif"
30: "http://www.huffingtonpost.com/images/trans.gif"
31: "http://images.huffingtonpost.com/2012-04-06-Screenshot20120406at7.09.17PM.jpg"
32: "http://www.huffingtonpost.com/images/trans.gif"
33: "http://www.huffingtonpost.com/images/trans.gif"
34: "http://www.huffingtonpost.com/images/trans.gif"
35: "http://www.huffingtonpost.com/images/trans.gif"
36: "http://www.huffingtonpost.com/images/trans.gif"
37: "http://www.huffingtonpost.com/images/trans.gif"
38: "http://www.huffingtonpost.com/images/trans.gif"
39: "http://www.huffingtonpost.com/images/trans.gif"
40: "http://www.huffingtonpost.com/images/trans.gif"
41: "http://www.huffingtonpost.com/images/trans.gif"
42: "http://www.huffingtonpost.com/images/trans.gif"
43: "http://www.huffingtonpost.com/images/trans.gif"
44: "http://www.huffingtonpost.com/images/trans.gif"
45: "http://www.huffingtonpost.com/images/trans.gif"
46: "http://www.huffingtonpost.com/images/trans.gif"
47: "http://www.huffingtonpost.com/images/trans.gif"
48: "http://www.huffingtonpost.com/images/trans.gif"
49: "http://www.huffingtonpost.com/images/trans.gif"
50: "http://www.huffingtonpost.com/images/trans.gif"
51: "http://www.huffingtonpost.com/images/trans.gif"
52: "http://www.huffingtonpost.com/images/trans.gif"
53: "http://www.huffingtonpost.com/images/trans.gif"
54: "http://www.huffingtonpost.com/images/trans.gif"
55: "http://www.huffingtonpost.com/images/trans.gif"
56: "http://www.huffingtonpost.com/images/trans.gif"
57: "http://www.huffingtonpost.com/images/trans.gif"
58: "http://www.huffingtonpost.com/images/trans.gif"
59: "http://www.huffingtonpost.com/images/trans.gif"
60: "http://www.huffingtonpost.com/images/trans.gif"
61: "http://www.huffingtonpost.com/images/trans.gif"
62: "http://www.huffingtonpost.com/images/trans.gif"
63: "http://www.huffingtonpost.com/images/trans.gif"
64: "http://www.huffingtonpost.com/images/trans.gif"
65: "http://www.huffingtonpost.com/images/trans.gif"
66: "http://www.huffingtonpost.com/images/trans.gif"
67: "http://www.huffingtonpost.com/images/trans.gif"
68: "http://www.huffingtonpost.com/images/trans.gif"
69: "http://www.huffingtonpost.com/images/trans.gif"
70: "http://www.huffingtonpost.com/images/trans.gif"
71: "http://www.huffingtonpost.com/images/trans.gif"
72: "http://www.huffingtonpost.com/images/trans.gif"
73: "http://www.huffingtonpost.com/images/trans.gif"
74: "http://www.huffingtonpost.com/images/trans.gif"
75: "http://s.huffpost.com/images/blank.gif"
76: "http://s.huffpost.com/images/blank.gif"
77: "http://s.huffpost.com/images/blank.gif"
78: "http://s.huffpost.com/images/blank.gif"
79: "http://s.huffpost.com/images/blank.gif"
80: "http://s.huffpost.com/images/blank.gif"
81: "http://s.huffpost.com/images/blank.gif"
82: "http://s.huffpost.com/images/facebook_promo_connect.png?3"
83: "http://s.huffpost.com/images/loader.gif"
84: "http://www.huffingtonpost.com/images/trans.gif"
85: "http://www.huffingtonpost.com/images/trans.gif"
86: "http://www.huffingtonpost.com/images/trans.gif"
87: "http://www.huffingtonpost.com/images/trans.gif"
88: "http://www.huffingtonpost.com/images/trans.gif"
89: "http://www.huffingtonpost.com/images/trans.gif"
90: "http://s.huffpost.com/contributors/gary-hart/headshot.jpg"
91: "http://s.huffpost.com/contributors/mike-campbell/headshot.jpg"
92: "http://s.huffpost.com/contributors/roma-downey/headshot.jpg"
93: "http://s.huffpost.com/contributors/gavin-newsom/headshot.jpg"
94: "http://s.huffpost.com/contributors/sarah-shourd/headshot.jpg"
95: "http://s.huffpost.com/contributors/jacqueline-novogratz/headshot.jpg"
96: "http://s.huffpost.com/contributors/peggy-drexler/headshot.jpg"
97: "http://s.huffpost.com/contributors/mohamed-a-elerian/headshot.jpg"
98: "http://s.huffpost.com/contributors/bill-mckibben/headshot.jpg"
99: "http://s.huffpost.com/contributors/marlo-thomas/headshot.jpg"
100: "http://www.huffingtonpost.com/images/v/something_to_say_button.png"
101: "http://www.huffingtonpost.com/images/trans.gif"
102: "http://www.huffingtonpost.com/images/trans.gif"
103: "http://www.huffingtonpost.com/images/trans.gif"
104: "http://www.huffingtonpost.com/images/trans.gif"
105: "http://www.huffingtonpost.com/images/trans.gif"
106: "http://www.huffingtonpost.com/images/trans.gif"
107: "http://www.huffingtonpost.com/images/trans.gif"
108: "http://www.huffingtonpost.com/images/trans.gif"
109: "http://www.huffingtonpost.com/images/trans.gif"
110: "http://www.huffingtonpost.com/images/trans.gif"
111: "http://www.huffingtonpost.com/images/trans.gif"
112: "http://www.huffingtonpost.com/images/trans.gif"
113: "http://www.huffingtonpost.com/images/trans.gif"
114: "http://www.huffingtonpost.com/images/trans.gif"
115: "http://www.huffingtonpost.com/images/trans.gif"
116: "http://www.huffingtonpost.com/images/trans.gif"
117: "http://www.huffingtonpost.com/images/trans.gif"
118: "http://www.huffingtonpost.com/images/trans.gif"
119: "http://www.huffingtonpost.com/images/trans.gif"
120: "http://www.huffingtonpost.com/images/trans.gif"
121: "http://www.huffingtonpost.com/images/trans.gif"
122: "http://www.huffingtonpost.com/images/trans.gif"
123: "http://www.huffingtonpost.com/images/trans.gif"
124: "http://www.huffingtonpost.com/images/trans.gif"
125: "http://www.huffingtonpost.com/images/trans.gif"
126: "http://www.huffingtonpost.com/images/trans.gif"
127: "http://www.huffingtonpost.com/images/trans.gif"
128: "http://www.huffingtonpost.com/images/trans.gif"
129: "http://www.huffingtonpost.com/images/trans.gif"
130: "http://www.huffingtonpost.com/images/trans.gif"
131: "http://www.huffingtonpost.com/images/trans.gif"
132: "http://www.huffingtonpost.com/images/trans.gif"
133: "http://www.huffingtonpost.com/images/trans.gif"
134: "http://b.scorecardresearch.com/p?c1=2&c2=6723616&c3=&c4=&c5=front&c6=&c15=&cj=1"
135: "http://www.huffingtonpost.com//secure-us.imrworldwide.com/cgi-bin/m?ci=us-703240h&cg=0&cc=1&ts=noscript"
136: "http://vertical-stats.huffpost.com/?-1&&"
137: "http://www.huffingtonpost.com//pixel.quantserve.com/pixel/p-6fTutip1SMLM2.gif?labels=Home"
images_count: 138
redirected: false
status: "success"
title: "Breaking News and Opinion on The Huffington Post"
type: "text/html; charset=utf-8"
幾乎是伺服器返回的同時,瀏覽器開始載入圖片。chrome監控如下。黃色的那個線表示提交url擷取圖片資源,後面的就都是載入圖片了,載入的速度還是取決於我這兒的網路。
由於http://pinterest.com/的JS代碼經過壓縮,且使用了JQuery,所以找起來特別費勁。其實具體怎麼幹就很簡單,誰都能想到。遍曆json資料,建立img標籤對象,設定src屬性,儲存對象。剩下的瀏覽器就會自己完成。
------解決思路----------------------
引用:
引用:
剛註冊了http://pinterest.com。 它的做法就是用用戶端來載入
點擊Add 選擇Pin ,貼上網址 http://www.huffingtonpost.com/
在chrome的Network中可以看到有一個請求
GET /pin/create/find_images/?url=http%253A%2F%2Fwww.huffingtonpo……
什麼對象?
你是說伺服器返回的image連結的資料嗎?不用儲存呀。收到ajax請求後解析返回資料就完了
另外,瀏覽器載入外部資源都是非同步。也就是說,不管是不是用的JQuery,都是非同步載入的,相互不會影響。和老大寫的php端的差不多。