{"record":{"id":"bba5012cb472cd54","repo":"PHPOffice/PHPWord","slug":"invalid-image-s","errorCode":null,"errorMessage":"Invalid image: %s","messagePattern":"Invalid image: (.+?)","errorType":"exception","errorClass":"InvalidImageException","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Element/Image.php","lineNumber":473,"sourceCode":"    }\n\n    /**\n     * Check memory image, supported type, image functions, and proportional width/height.\n     */\n    private function checkImage(): void\n    {\n        $this->setSourceType();\n\n        // Check image data\n        if ($this->sourceType == self::SOURCE_ARCHIVE) {\n            $imageData = $this->getArchiveImageSize($this->source);\n        } elseif ($this->sourceType == self::SOURCE_STRING) {\n            $imageData = @getimagesizefromstring($this->source);\n        } else {\n            $imageData = @getimagesize($this->source);\n        }\n        if (!is_array($imageData)) {\n            throw new InvalidImageException(sprintf('Invalid image: %s', $this->source));\n        }\n        [$actualWidth, $actualHeight, $imageType] = $imageData;\n\n        // Check image type support\n        $supportedTypes = [IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG];\n        if ($this->sourceType != self::SOURCE_GD && $this->sourceType != self::SOURCE_STRING) {\n            $supportedTypes = array_merge($supportedTypes, [IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM]);\n        }\n        if (!in_array($imageType, $supportedTypes)) {\n            throw new UnsupportedImageTypeException();\n        }\n\n        // Define image functions\n        $this->imageType = image_type_to_mime_type($imageType);\n        $this->setFunctions();\n        $this->setProportionalSize($actualWidth, $actualHeight);\n    }\n","sourceCodeStart":455,"sourceCodeEnd":491,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Element/Image.php#L455-L491","documentation":"Image::checkImage() probes the source with getimagesize()/getimagesizefromstring(); when PHP cannot read any image metadata (not an image, unreadable path, corrupt data) it throws InvalidImageException with the source in the message. The library only proceeds with images PHP can identify.","triggerScenarios":"new Image($section, $pathOrUrlOrString) where the file doesn't exist/is unreadable, the content is not a recognized image, the URL is unreachable, or the string is not raw image bytes.","commonSituations":"Typos in file paths, 404 image URLs, uploading validation gaps letting non-image files through, downloading images over HTTP that returned an HTML error page instead of bytes.","solutions":["Verify the file exists and is readable: is_readable($path) before constructing the Image.","Validate the source is a real image: check @getimagesize($source) or finfo MIME type yourself first.","For URLs, download with error handling and confirm the response body is image bytes (not an error page).","Wrap Image construction in try-catch (InvalidImageException) when the source is user-supplied."],"exampleFix":"// before\n$image = $section->addImage($userFile);\n// after\nif (!is_array(@getimagesize($userFile))) { throw new Exception('Not a valid image'); }\n$image = $section->addImage($userFile);","handlingStrategy":"validation","validationCode":"if (is_string($source)) {\n    if (filter_var($source, FILTER_VALIDATE_URL)) {\n        $bytes = @file_get_contents($source);\n        if ($bytes === false || @getimagesizefromstring($bytes) === false) { throw new DomainException('Not a valid image: ' . $source); }\n    } elseif (!is_file($source) || @getimagesize($source) === false) {\n        throw new DomainException('Not a valid image: ' . $source);\n    }\n}","typeGuard":"function isReadableImageSource($source): bool { return is_string($source) && (@getimagesize($source) !== false || @getimagesizefromstring($source) !== false); }","tryCatchPattern":"try { $img = $section->addImage($source); } catch (\\PhpOffice\\PhpWord\\Exception\\InvalidImageException $e) { $img = null; log('bad image: ' . $e->getMessage()); }","preventionTips":["Check file_exists/is_readable before adding images","Download remote images and verify bytes instead of passing URLs blindly","Validate uploads are real images (getimagesize/finfo) before storage","Handle 404/error-page responses when fetching images over HTTP"],"tags":["phpword","image","file-not-found","validation"],"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"}