sebastiansieber
sebastiansieber

Reputation: 63

PHP: get largest image - performance issue

The below code finds all the images on a given website and returns the largest, BUT it is terribly slow. Can anyone help? Thanks!

$largest = 0;
$images = array();

reg_match_all('/(img|src)\=(\"|\')[^\"\'\>]+/i', $html, $media);
unset($html);

$data=preg_replace('/(img|src)(\"|\'|\=\"|\=\')(.*)/i',"$3",$media[0]);
foreach($data as $imgUrl) {
$info = pathinfo($imgUrl);

if (isset($info['extension'])) {
    if (($info['extension'] == 'jpg') ||
        ($info['extension'] == 'jpeg') ||
        ($info['extension'] == 'gif') ||
        ($info['extension'] == 'png')) {

        array_push($images, $imgUrl);
        $imgUrl = $this->rel2abs($imgUrl, $this->url);
        list($width, $height, $type, $attr) = getimagesize($imgUrl);
        $size = $width * $height;

        if ($size > $largest) {
            $this->image = $imgUrl;
            $largest = $size;
        }
    }
}

Upvotes: 0

Views: 315

Answers (1)

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

I had same issue as you, the problem is that getimagesize is downloading image back to server then gets it's size, so i have written my own wrapper, who only reads img meta information and returns size back.
Here: http://pastebin.com/3XuSAw0q is class. It can handle jpg, png and gif files.

Use it like:

$image = new Parser_Provider_Image();
$sizes = $image->getImageSize('http://../../img.jpg');

Upvotes: 4

Related Questions