2010-08-31 82 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获取图像,什么是缓慢的过程。

您应该只在第一次获得它的大小并将其保存在未来的数据库中。