不知道大家有沒有玩過 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屬性,就完全可以擷取圖片的原始大小。
好處多多,不過可能的麻煩是防盜鏈
讀取並解析 2.8秒
讀取圖片(138個) 27秒
找到 7 個
僅從最佳化代碼出發,應該油水不大
可考慮多路並發
讀取並解析 3.6秒
啟動讀取圖片進程(138個) 1.3秒
結果檔案中記錄數 7 個
http://s.huffpost.com/images/v/logos/v4/tagline.gifhttp://s.huffpost.com/images/v/logos/v4/homepage.gif?v9http://i.huffpost.com/gen/559399/thumbs/r-OLBERMANN-huge.jpghttp://s.huffpost.com/images/facebook_promo_connect.png?3http://images.huffingtonpost.com/2012-04-04-michaeljfoxmarlo2SECOND.jpghttp://images.huffingtonpost.com/2012-04-05-Screenshot20120405at9.40.24AM.jpghttp://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不夠嗎
每天回帖即可獲得10分可用分
學習! 是用PHP擷取圖片url後直接讀取圖片的頭資訊嗎?
學習了,可能以後會用到
好深奧
估計很快就會用到了……
pinterest那個pin功能創意很好,而且技術很簡單,就是書籤一串js代碼,然後你點這個書籤就相當於往當前頁面文檔append入一個js檔案,這個js檔案怎麼寫,就很簡單了,主要就是遍曆document.getElementsByTagName('img')
啊,貌似LZ說的是另一個功能,我看錯了。
to xuzuning: 我用的是apache2,不是iis6
138個照片並發,是不是就消耗了138個串連數?是否需要修改php.ini,增加串連數?此外,CPU和記憶體開銷如何?謝謝。
to dream1206,yiwusuo,amani11: 剛才又琢磨了一下他的添加。貌似提交網址後,第一時間(1-3秒內)先返回一張圖片,然後在(7-9秒後)返回剩餘的圖片資訊。應該是你們說的那種PHP只擷取所有的圖片地址,JS判斷圖片大小,甚至ajax並發傳輸到第二個PHP頁面,判斷圖片長寬後返回資料。
但是不論如何,並發是少不了的。用JS並發和直接PHP並發,2者從資源消耗角度來比,哪個會更少?謝謝。
138個照片並發,是不是就消耗了138個串連數
對
是否需要修改php.ini,增加串連數
否,串連是向外的,如果要改,也是對方改
CPU和記憶體開銷如何
這個不太好測試
又,關於使用 js 判斷的問題,由於他們沒有給出代碼,無法測試
自己寫了兩個方案都不理想,也就作罷了
用JS並發和直接PHP並發,2者從資源消耗角度來比,哪個會更少
資源消耗角度來比 都一樣,都要完整的載入圖片
不過前者是消耗用戶端資源,後者是消耗伺服器端資源
另外瀏覽器的機制不很瞭解,是否真的是並發也未可知
謝謝 xuzuning的詳解。 繼續討論。 另一個論壇上同有一位高手解答,轉帖代碼。
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 "";print_r ( imageDownload ( $nodes, 200, 200 ) );echo "", microtime () - $start, "
";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 [0] >= $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);}
執行時間4.8秒。但是 if(in_array($absUrl, $visited))continue; 這行報錯。 Warning: in_array() expects parameter 2 to be array, null。 此外最終圖片地址並非網路地址,而是本機快取地址。
進一步測試研究。
這段代碼在我這裡大約 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://www.planeart.cn/?p=1121
很受用!
不錯,PHP強大!!
學習了!
每天回帖即可獲得10分可用分
剛才又去找了一下,終於還是把那個文章找到了,你可以去學習一下~
http://www.planeart.cn/?p=1121
你就不能做個範例程式碼嗎? 剛註冊了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: 138redirected: falsestatus: "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.huffingtonpost.com HTTP/1.……
這方面對象儲存在哪裡呢?cookie裡,還是伺服器裡的曆史檔案?此外jquery如何多線程擷取圖片長和寬?
引用 38 樓 的回複:
剛註冊了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端的差不多。
引用 29 樓 的回複:
剛才又去找了一下,終於還是把那個文章找到了,你可以去學習一下~
http://www.planeart.cn/?p=1121
你就不能做個範例程式碼嗎?
http://www.planeart.cn/demo/imgReady/ 單元以後能看懂
樓主辛苦支援一下。。
樓主辛苦支援一下。。
+1+1+1+1+1+1