PHPePub\Core\EPub::resolveMedia PHP Method

resolveMedia() protected method

Resolve a media src and determine it's target location and add it to the book.
protected resolveMedia ( string $source, string &$internalPath, string &$internalSrc, string &$isSourceExternal, string $baseDir = "", string $htmlDir = "" ) : boolean
$source string Source link.
$internalPath string (referenced) Return value, will be set to the target path and name in the book.
$internalSrc string (referenced) Return value, will be set to the target name in the book.
$isSourceExternal string (referenced) Return value, will be set to TRUE if the image originated from a full URL.
$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.
return boolean
    protected function resolveMedia($source, &$internalPath, &$internalSrc, &$isSourceExternal, $baseDir = "", $htmlDir = "")
    {
        if ($this->isFinalized) {
            return false;
        }
        $mediaPath = null;
        $tmpFile = null;
        if (preg_match('#^(http|ftp)s?://#i', $source) == 1) {
            $urlInfo = parse_url($source);
            if (strpos($urlInfo['path'], $baseDir . "/") !== false) {
                $internalSrc = substr($urlInfo['path'], strpos($urlInfo['path'], $baseDir . "/") + strlen($baseDir) + 1);
            }
            $internalPath = $urlInfo["scheme"] . "/" . $urlInfo["host"] . "/" . pathinfo($urlInfo["path"], PATHINFO_DIRNAME);
            $isSourceExternal = true;
            $mediaPath = FileHelper::getFileContents($source, true);
            $tmpFile = $mediaPath;
        } else {
            if (strpos($source, "/") === 0) {
                $internalPath = pathinfo($source, PATHINFO_DIRNAME);
                $mediaPath = $source;
                if (!file_exists($mediaPath)) {
                    $mediaPath = $this->docRoot . $mediaPath;
                }
            } else {
                $internalPath = $htmlDir . "/" . preg_replace('#^[/\\.]+#', '', pathinfo($source, PATHINFO_DIRNAME));
                $mediaPath = $baseDir . "/" . $source;
                if (!file_exists($mediaPath)) {
                    $mediaPath = $this->docRoot . $mediaPath;
                }
            }
        }
        if ($mediaPath !== false) {
            $mime = MimeHelper::getMimeTypeFromExtension(pathinfo($source, PATHINFO_EXTENSION));
            $internalPath = RelativePath::getRelativePath("media/" . $internalPath . "/" . $internalSrc);
            if (!array_key_exists($internalPath, $this->fileList) && $this->addLargeFile($internalPath, "m_" . $internalSrc, $mediaPath, $mime)) {
                $this->fileList[$internalPath] = $source;
            }
            if (isset($tmpFile)) {
                unlink($tmpFile);
            }
            return true;
        }
        return false;
    }