2010-08-31 76 views
0

我需要在我的網站上顯示多個職位。這些帖子由內部和外部帖子組合而成。定期導入外部帖子並使用cronjob將其保存在我的數據庫中。慢getimagesize

在顯示帖子之前,我從所有HTML中提取文本。另外,我嘗試找到帖子中包含的第一張圖片,直到找到寬度符合我要求的圖片。 (我只顯示文本的小版本,以及來自帖子的一張圖片作爲傳情)

爲了找到最合適的圖片,我使用了getimagesize,但不幸的是這通常會創建幾秒的PHP執行時間!

如何加快我的功能?我絕望的技巧和好的調整方法!

在此先感謝

//extract text without tags from blog post 
$content = str_get_html("".$post_text."")->plaintext; 

$max_width = 475; 
$picture_id = 0; 

//fetch images from blog post 
foreach($html->find('img') as $e) { 

//get picture attributes 
list($width, $height, $type, $attr) = getimagesize((is_absolute_url($e->src) ? $e->src : $_SERVER['DOCUMENT_ROOT'].$e->src)); 

//adjust image width & height, so it's the size of the page 
$new_width = $max_width; 
$new_height = $new_width/$width * $height; 

//find percentage of current width versus max width 
$percentage = ($width/$max_width) * 100; 

    //select picture for display and resizing if the picture is large enough (we don't want to stretch it too much) 
    if($percentage >= 60) { 

     $e->width = $new_width; 
     $e->height = $new_height; 

     $picture = array('src' => $e->src, 'width' => $e->width, 'height' => $e->height); 

     //stop after first picture is found :: we only need one per post 
     if (++$picture_id == 1) break; 

    } 

} 
+1

它看起來像你通過HTTP查詢圖像。這可能是你的問題。不知道是否有可能以任何方式加速。 – 2010-08-31 13:40:45

回答

4

原因:這是一個很好的已知問題,getimagesize作品上遠程文件緩慢。

解決方案:建議將文件存儲在本地服務器上(臨時),然後在其上執行getimagesize

+3

此外,將圖像刮取過程包含在您的cron作業中。 – 2010-08-31 13:51:56

2

當您將url作爲參數傳遞給getimagesize時,它通過HTTP獲取圖像,什麼是緩慢的過程。

您應該只在第一次獲得它的大小並將其保存在未來的數據庫中。