1

我需要在我的网站上显示多个帖子。这些职位包括内部和外部职位。使用 cronjob 定期导入外部帖子并保存在我的数据库中。

在显示帖子之前,我从所有 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;

    }

}
4

2 回答 2

4

原因:这是一个众所周知的问题,getimagesize在远程文件上运行缓慢。

解决方案:建议将文件(临时)存储在本地服务器上,然后getimagesize在其上执行。

于 2010-08-31T13:44:34.037 回答
2

当你将一个url作为参数传递给getimagesize时,它通过HTTP获取图像,这是一个缓慢的过程。

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

于 2010-08-31T13:54:03.160 回答