{"record":{"id":"551bbb3c268d08f1","repo":"PHPOffice/PHPWord","slug":"could-not-load-image-originsrc","errorCode":null,"errorMessage":"Could not load image $originSrc","messagePattern":"Could not load image \\$originSrc","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Shared/Html.php","lineNumber":1052,"sourceCode":"                preg_match('/.+\\.(\\w+)$/', $src, $match);\n                $src = $tmpDir . uniqid();\n                if (isset($match[1])) {\n                    $src .= '.' . $match[1];\n                }\n\n                $ifp = fopen($src, 'wb');\n\n                if ($ifp !== false) {\n                    fwrite($ifp, $imgBlob);\n                    fclose($ifp);\n                }\n            }\n        }\n\n        if (is_file($src)) {\n            $newElement = $element->addImage($src, $style, false, null, $altText);\n        } else {\n            throw new Exception(\"Could not load image $originSrc\");\n        }\n\n        return $newElement;\n    }\n\n    /**\n     * Transforms a CSS border style into a word border style.\n     *\n     * @param string $cssBorderStyle\n     *\n     * @return null|string\n     */\n    protected static function mapBorderStyle($cssBorderStyle)\n    {\n        switch ($cssBorderStyle) {\n            case 'none':\n            case 'dashed':\n            case 'dotted':","sourceCodeStart":1034,"sourceCodeEnd":1070,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Shared/Html.php#L1034-L1070","documentation":"Html::parseImage() resolves an <img> src to a local path (or downloads it) and, if is_file($src) fails, it cannot obtain the image binary and throws Exception. The message shows the original src attribute, not the resolved path, which can be misleading.","triggerScenarios":"Parsing HTML via the HTML reader / addHtml with an <img> tag whose src is a remote URL when downloading failed or was not performed, a relative path that does not resolve from the document's base directory, or a path to a nonexistent/removed file.","commonSituations":"Embedding HTML snippets with remote CDN images; moving HTML files so relative image paths break; images generated at runtime but not yet written to disk; open_basedir or permissions blocking file access.","solutions":["Verify the image path exists and is readable with is_file()/is_readable() before adding the HTML.","Replace remote URLs with locally downloaded copies (e.g. via file_get_contents/curl) and reference local paths.","Make relative src paths absolute relative to the correct base directory.","Check open_basedir/permissions if the file definitely exists."],"exampleFix":"// before\n$section->addHtml('<img src=\"https://cdn.example.com/logo.png\">');\n// after\n$local = sys_get_temp_dir() . '/logo.png';\nfile_put_contents($local, file_get_contents('https://cdn.example.com/logo.png'));\n$section->addImage($local);","handlingStrategy":"validation","validationCode":"if (!is_file($src) && !filter_var($src, FILTER_VALIDATE_URL)) {\n    throw new InvalidArgumentException(\"Image not accessible: $src\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    $section->addHtml($html);\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if (str_starts_with($e->getMessage(), 'Could not load image')) {\n        $logger->warning('Skipping unresolvable image', ['msg' => $e->getMessage()]);\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Pre-download remote images and reference local paths","Make relative src paths absolute before parsing HTML","Check is_file()/is_readable() for every image referenced in generated HTML"],"tags":["phpword","image","html-parsing","file-not-found"],"backgroundTag":"file-not-found","analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}