首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >缓慢的getimagesize

缓慢的getimagesize
EN

Stack Overflow用户
提问于 2010-08-31 21:37:22
回答 2查看 4.3K关注 0票数 1

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

在显示帖子之前,我从所有HTML中提取文本。此外,我尝试定位包含在帖子中的第一个图像,直到我找到一个高度和宽度符合我的要求的图像。(我只展示了文本的一个小版本,以及帖子中的一张图片作为预告片)

为了找到最合适的图片,我使用了getimagesize,但不幸的是,这通常会产生几秒钟的PHP执行时间!

我怎样才能加速我下面的函数?我迫切需要小贴士和好的调整方法!

提前感谢

代码语言:javascript
复制
//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;

    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-31 21:44:34

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

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

票数 4
EN

Stack Overflow用户

发布于 2010-08-31 21:54:03

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3609510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档