PHPOffice/PHPWord · error · Exception
Could not load image $originSrc
Error message
Could not load image $originSrc
What it means
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.
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.
Example fix
// before
$section->addHtml('<img src="https://cdn.example.com/logo.png">');
// after
$local = sys_get_temp_dir() . '/logo.png';
file_put_contents($local, file_get_contents('https://cdn.example.com/logo.png'));
$section->addImage($local); Defensive patterns
Strategy: validation
Validate before calling
if (!is_file($src) && !filter_var($src, FILTER_VALIDATE_URL)) {
throw new InvalidArgumentException("Image not accessible: $src");
} Try / catch
try {
$section->addHtml($html);
} catch (\PhpOffice\PhpWord\Exception\Exception $e) {
if (str_starts_with($e->getMessage(), 'Could not load image')) {
$logger->warning('Skipping unresolvable image', ['msg' => $e->getMessage()]);
} else {
throw $e;
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.
Related errors
- Invalid image
- Could not open ' . $sFileName . ' for reading! File does…
- Cannot find archive file.
- Invalid image
- Invalid value, on of ' . implode(', ', $position) . '…
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/551bbb3c268d08f1.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/Shared/Html.php:1052
preg_match('/.+\.(\w+)$/', $src, $match);
$src = $tmpDir . uniqid();
if (isset($match[1])) {
$src .= '.' . $match[1];
}
$ifp = fopen($src, 'wb');
if ($ifp !== false) {
fwrite($ifp, $imgBlob);
fclose($ifp);
}
}
}
if (is_file($src)) {
$newElement = $element->addImage($src, $style, false, null, $altText);
} else {
throw new Exception("Could not load image $originSrc");
}
return $newElement;
}
/**
* Transforms a CSS border style into a word border style.
*
* @param string $cssBorderStyle
*
* @return null|string
*/
protected static function mapBorderStyle($cssBorderStyle)
{
switch ($cssBorderStyle) {
case 'none':
case 'dashed':
case 'dotted':View on GitHub (pinned to aef95c0415)