{"record":{"id":"eddd1d1d96fe6af6","repo":"PHPOffice/PHPWord","slug":"invalid-image-s-templateprocessor","errorCode":null,"errorMessage":"Invalid image: %s","messagePattern":"Invalid image: (.+?)","errorType":"exception","errorClass":"PhpOffice\\PhpWord\\Exception\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/TemplateProcessor.php","lineNumber":568,"sourceCode":"            if (isset($replaceImage['width'])) {\n                $width = $replaceImage['width'];\n            }\n            if (isset($replaceImage['height'])) {\n                $height = $replaceImage['height'];\n            }\n            if (isset($replaceImage['ratio'])) {\n                $ratio = $replaceImage['ratio'];\n            }\n        } else {\n            $imgPath = $replaceImage;\n        }\n\n        $width = $this->chooseImageDimension($width, $varInlineArgs['width'] ?? null, 115);\n        $height = $this->chooseImageDimension($height, $varInlineArgs['height'] ?? null, 70);\n\n        $imageData = @getimagesize($imgPath);\n        if (!is_array($imageData)) {\n            throw new Exception(sprintf('Invalid image: %s', $imgPath));\n        }\n        [$actualWidth, $actualHeight, $imageType] = $imageData;\n\n        // fix aspect ratio (by default)\n        if (null === $ratio && isset($varInlineArgs['ratio'])) {\n            $ratio = $varInlineArgs['ratio'];\n        }\n        if (null === $ratio || !in_array(strtolower($ratio), ['', '-', 'f', 'false'])) {\n            $this->fixImageWidthHeightRatio($width, $height, $actualWidth, $actualHeight);\n        }\n\n        $imageAttrs = [\n            'src' => $imgPath,\n            'mime' => image_type_to_mime_type($imageType),\n            'width' => $width,\n            'height' => $height,\n        ];\n","sourceCodeStart":550,"sourceCodeEnd":586,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/TemplateProcessor.php#L550-L586","documentation":"TemplateProcessor::prepareImageAttrs verifies an image file by calling getimagesize on the resolved path; if it does not return an array (file missing, unreadable, or not a real image), it throws 'Invalid image: %s' with the path. This guards the placeholder-replacement image feature against unusable image files.","triggerScenarios":"setImageValue('${placeholder}', ['path' => '/missing.png']) when the file does not exist, the path is wrong, the file is corrupted or not a real image (e.g. renamed text file), or PHP lacks permission to read it.","commonSituations":"Relative paths resolved against a wrong working directory (CLI vs web server); files deleted between upload and template processing; images generated by an earlier step that failed silently; unsupported/legacy formats like some TIFF/WebP depending on the PHP build.","solutions":["Verify the path with is_file and that getimagesize() returns an array before calling setImageValue","Use an absolute path or resolve relative to __DIR__","Re-generate or re-save the image if the file is corrupt (open it with an image editor to confirm)","Check read permissions on the image file"],"exampleFix":"// before\n$template->setImageValue('${logo}', ['path' => 'logo.png']);\n// after\n$path = __DIR__ . '/logo.png';\nif (!is_file($path) || getimagesize($path) === false) {\n    throw new RuntimeException('Image missing or invalid: ' . $path);\n}\n$template->setImageValue('${logo}', ['path' => $path]);","handlingStrategy":"validation","validationCode":"$path = __DIR__ . '/logo.png';\nif (!is_file($path) || !is_readable($path) || getimagesize($path) === false) {\n    throw new RuntimeException(\"Image invalid or unreadable: $path\");\n}\n$template->setImageValue('${logo}', ['path' => $path]);","typeGuard":"function isUsableImage(?string $path): bool {\n    return $path !== null && is_file($path) && is_readable($path) && is_array(@getimagesize($path));\n}","tryCatchPattern":"try {\n    $template->setImageValue('${logo}', ['path' => $imgPath]);\n} catch (\\PhpOffice\\PhpWord\\Exception\\Exception $e) {\n    if (str_starts_with($e->getMessage(), 'Invalid image:')) {\n        // log path, substitute a placeholder image or skip\n    }\n}","preventionTips":["Resolve image paths absolutely (relative paths depend on CWD)","Confirm uploads are real images with getimagesize before storing","Check file existence right before template processing","Handle async upload/generation failures instead of failing silently"],"tags":["image","template","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"}