PressBooks\Modules\Import\Html\Xhtml::scrapeAndKneadImages PHP Метод

scrapeAndKneadImages() защищенный Метод

Parse HTML snippet, save all found tags using media_handle_sideload(), return the HTML with changed paths.
protected scrapeAndKneadImages ( DOMDocument $doc, string $domain ) : DOMDocument
$doc DOMDocument
$domain string domain name of the webpage
Результат DOMDocument
    protected function scrapeAndKneadImages(\DOMDocument $doc, $domain)
    {
        $images = $doc->getElementsByTagName('img');
        foreach ($images as $image) {
            // Fetch image, change src
            $old_src = $image->getAttribute('src');
            // change to absolute links, if relative found
            if (false === strpos($old_src, 'http')) {
                $old_src = $domain . '/' . $old_src;
            }
            $new_src = $this->fetchAndSaveUniqueImage($old_src);
            if ($new_src) {
                // Replace with new image
                $image->setAttribute('src', $new_src);
            } else {
                // Tag broken image
                $image->setAttribute('src', "{$old_src}#fixme");
            }
        }
        return $doc;
    }