我需要在我的网站上显示多个帖子。这些职位包括内部和外部职位。使用 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;
}
}