views:

29

answers:

2

Hi all,

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;

    }

}
+2  A: 

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.

shamittomar
Also, include the image scraping process to your cron job.
nikc
A: 

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.

NeDark