Categories
PHP

Extract images from an HTML snippet

The function here will take an HTML fragment and return an array of useful images it finds.

';
    $text = $header . $text;
    $dom = new DOMDocument();
    if (@$dom->loadHTML($text)) {
        $xpath = new DOMXpath($dom);
        if ($images = $xpath->evaluate("//img")) {
            $result = array();
            foreach ($images as $i => $img) {
                $ht = $img->getAttribute('height');
                $wd = $img->getAttribute('width');
                // if height & width are 1 its a bug, ignore
                if (1 === (int)$ht && 1 === (int)$wd) {
                    continue;
                }
                // if it doesn't end in an image file extension
                // then ignore
                $src = $img->getAttribute('src');
                if (!preg_match('/.(png|jpg|gif)$/i', $src)) {
                    continue;
                }
                // do we need to figure out the full url to the image?
                if (!preg_match('/^https?:///', $src)) {
                    continue;
                }
                $alt = $img->getAttribute('alt');
                $result[$i] = array('src' => $src, 'alt' => $alt, 'height' => $ht, 'width' => $wd);
            }
            if (!empty($result)) {
                return $result;
            }
        }
    }
    return false;
}