PHPePub\Core\EPub::processChapterImages PHP Method

processChapterImages() protected method

$externalReferences will determine what will happen to these images, and the img src will be rewritten accordingly.
protected processChapterImages ( &$xmlDoc, integer $externalReferences = EPub::EXTERNAL_REF_ADD, string $baseDir = "", string $htmlDir = "", string $backPath = "" ) : boolean
$externalReferences integer How to handle external references, EPub::EXTERNAL_REF_IGNORE, EPub::EXTERNAL_REF_ADD or EPub::EXTERNAL_REF_REMOVE_IMAGES? Default is EPub::EXTERNAL_REF_ADD.
$baseDir string Default is "", meaning it is pointing to the document root.
$htmlDir string The path to the parent HTML file's directory from the root of the archive.
$backPath string The path to get back to the root of the archive from $htmlDir.
return boolean FALSE if uncuccessful (book is finalized or $externalReferences == EXTERNAL_REF_IGNORE).
    protected function processChapterImages(&$xmlDoc, $externalReferences = EPub::EXTERNAL_REF_ADD, $baseDir = "", $htmlDir = "", $backPath = "")
    {
        if ($this->isFinalized || $externalReferences === EPub::EXTERNAL_REF_IGNORE) {
            return false;
        }
        // process img tags.
        $postProcDomElememts = array();
        $images = $xmlDoc->getElementsByTagName("img");
        $itemCount = $images->length;
        for ($idx = 0; $idx < $itemCount; $idx++) {
            /** @var $img \DOMElement */
            $img = $images->item($idx);
            if ($externalReferences === EPub::EXTERNAL_REF_REMOVE_IMAGES) {
                $postProcDomElememts[] = $img;
            } else {
                if ($externalReferences === EPub::EXTERNAL_REF_REPLACE_IMAGES) {
                    $altNode = $img->attributes->getNamedItem("alt");
                    $alt = "image";
                    if ($altNode !== null && strlen($altNode->nodeValue) > 0) {
                        $alt = $altNode->nodeValue;
                    }
                    $postProcDomElememts[] = array($img, StringHelper::createDomFragment($xmlDoc, "<em>[" . $alt . "]</em>"));
                } else {
                    $source = $img->attributes->getNamedItem("src")->nodeValue;
                    $parsedSource = parse_url($source);
                    $internalSrc = FileHelper::sanitizeFileName(urldecode(pathinfo($parsedSource['path'], PATHINFO_BASENAME)));
                    $internalPath = "";
                    $isSourceExternal = false;
                    if ($this->resolveImage($source, $internalPath, $internalSrc, $isSourceExternal, $baseDir, $htmlDir)) {
                        $img->setAttribute("src", $backPath . $internalPath);
                    } else {
                        if ($isSourceExternal) {
                            $postProcDomElememts[] = $img;
                            // External image is missing
                        }
                    }
                    // else do nothing, if the image is local, and missing, assume it's been generated.
                }
            }
        }
        foreach ($postProcDomElememts as $target) {
            if (is_array($target)) {
                $target[0]->parentNode->replaceChild($target[1], $target[0]);
            } else {
                $target->parentNode->removeChild($target);
            }
        }
        return true;
    }