PHPOffice/PHPWord · error · InvalidImageException

Invalid image

Error message

Invalid image: %s

What it means

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.

Solutions

  1. Verify the file exists and is readable: is_readable($path) before constructing the Image.
  2. Validate the source is a real image: check @getimagesize($source) or finfo MIME type yourself first.
  3. For URLs, download with error handling and confirm the response body is image bytes (not an error page).
  4. Wrap Image construction in try-catch (InvalidImageException) when the source is user-supplied.

Example fix

// before
$image = $section->addImage($userFile);
// after
if (!is_array(@getimagesize($userFile))) { throw new Exception('Not a valid image'); }
$image = $section->addImage($userFile);
Defensive patterns

Strategy: validation

Validate before calling

if (is_string($source)) {
    if (filter_var($source, FILTER_VALIDATE_URL)) {
        $bytes = @file_get_contents($source);
        if ($bytes === false || @getimagesizefromstring($bytes) === false) { throw new DomainException('Not a valid image: ' . $source); }
    } elseif (!is_file($source) || @getimagesize($source) === false) {
        throw new DomainException('Not a valid image: ' . $source);
    }
}

Type guard

function isReadableImageSource($source): bool { return is_string($source) && (@getimagesize($source) !== false || @getimagesizefromstring($source) !== false); }

Try / catch

try { $img = $section->addImage($source); } catch (\PhpOffice\PhpWord\Exception\InvalidImageException $e) { $img = null; log('bad image: ' . $e->getMessage()); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14). Data as JSON: /api/errors/bba5012cb472cd54. Report an issue: GitHub.

Appendix: source

Thrown at src/PhpWord/Element/Image.php:473

    }

    /**
     * Check memory image, supported type, image functions, and proportional width/height.
     */
    private function checkImage(): void
    {
        $this->setSourceType();

        // Check image data
        if ($this->sourceType == self::SOURCE_ARCHIVE) {
            $imageData = $this->getArchiveImageSize($this->source);
        } elseif ($this->sourceType == self::SOURCE_STRING) {
            $imageData = @getimagesizefromstring($this->source);
        } else {
            $imageData = @getimagesize($this->source);
        }
        if (!is_array($imageData)) {
            throw new InvalidImageException(sprintf('Invalid image: %s', $this->source));
        }
        [$actualWidth, $actualHeight, $imageType] = $imageData;

        // Check image type support
        $supportedTypes = [IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG];
        if ($this->sourceType != self::SOURCE_GD && $this->sourceType != self::SOURCE_STRING) {
            $supportedTypes = array_merge($supportedTypes, [IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM]);
        }
        if (!in_array($imageType, $supportedTypes)) {
            throw new UnsupportedImageTypeException();
        }

        // Define image functions
        $this->imageType = image_type_to_mime_type($imageType);
        $this->setFunctions();
        $this->setProportionalSize($actualWidth, $actualHeight);
    }

View on GitHub (pinned to aef95c0415)