缓慢获取图像大小

发布于 2024-09-16 20:41:32 字数 1374 浏览 12 评论 0原文

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

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

为了找到最合适的图片,我使用 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;

    }

}

I need to show multiple posts on my website. These posts are combined of internal and external posts. The external posts are periodically imported and saved in my DB using a cronjob.

Before showing the posts I extract the text from all HTML. In addition I try to locate the first image contained in the post, continuing until I find an image which height & width meets my requirements. (I only show a small version of the text, and one picture from the post as a teaser)

For the purpose of finding the most suitable picture, I use getimagesize, but unfortunately this often creates PHP Execution time of several seconds!

How can I speed up my function below? I'm desperate for tips and good tweaking methods!!

Thanks in advance

//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;

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲念泪 2024-09-23 20:41:32

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

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

Reason: It is a very well known issue that getimagesize works slow on remote files.

Solution: It is advised to store the files on your local server (temporarily) and then do getimagesize on it.

∝单色的世界 2024-09-23 20:41:32

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

您应该仅在第一次获取其大小并将其保存在数据库中以供将来使用。

When you pass a url as a parameter to getimagesize, it gets the image through HTTP, what is a slow process.

You should get its size only the first time and save it in a database for the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文