Intervention/image · error · Intervention\Image\Exceptions\AnalyzerException

Failed to read image size

Error message

Failed to read image size

What it means

Image::size() builds a Size object from width() and height() analyzer results; if the Size constructor rejects those values (a dimension < 0, see Size.php:36-46), the InvalidArgumentException is wrapped as AnalyzerException('Failed to read image size'). Note this specific message only appears for the Size-construction failure — a failing width()/height() analyzer itself propagates a different AnalyzerException from the driver.

Source

Thrown at src/Image.php:367

     */
    public function height(): int
    {
        return $this->analyze(new HeightAnalyzer());
    }

    /**
     * {@inheritdoc}
     *
     * @see ImageInterface::size()
     *
     * @throws AnalyzerException
     */
    public function size(): SizeInterface
    {
        try {
            return new Size($this->width(), $this->height());
        } catch (InvalidArgumentException $e) {
            throw new AnalyzerException('Failed to read image size', previous: $e);
        }
    }

    /**
     * {@inheritdoc}
     *
     * @see ImageInterface::colorspace()
     *
     * @throws AnalyzerException
     */
    public function colorspace(): ColorspaceInterface
    {
        return $this->analyze(new ColorspaceAnalyzer());
    }

    /**
     * {@inheritdoc}
     *

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Inspect getPrevious() — it carries the original InvalidArgumentException naming the negative dimension
  2. Discard and re-read the source from its original bytes; if it reproduces, the file itself is malformed
  3. Validate uploads (dimension sanity check right after read()) before feeding them into a processing pipeline
  4. If reproducible with a valid file, report a driver bug with the sample image

Example fix

// before
$size = $image->size(); // AnalyzerException on corrupted core

// after
try {
    $size = $image->size();
} catch (AnalyzerException $e) {
    $image = $manager->read($originalBytes); // re-read from source
    $size = $image->size();
}
Defensive patterns

Strategy: try-catch

Validate before calling

try {
    $size = $image->size(); // cheap pre-flight on untrusted images
} catch (AnalyzerException $e) {
    // reject/quarantine the source file here
}

Type guard

function hasReadableSize(ImageInterface $image): bool
{
    try { $image->size(); return true; }
    catch (\Intervention\Image\Exceptions\AnalyzerException) { return false; }
}

Try / catch

try {
    $size = $image->size();
} catch (AnalyzerException $e) {
    $root = $e->getPrevious(); // original Size InvalidArgumentException
    // discard image object; re-read from original bytes once
}

Prevention

When it happens

Trigger: An image whose driver core reports a negative width or height — e.g. corrupted image data, a malformed/crafted file that passed decoding, or a driver core left in an inconsistent state. Normal decoded or created images return dimensions >= 0 and never hit this.

Common situations: Processing untrusted uploads with hand-crafted headers, a partially-failed prior modification leaving a broken core, or driver edge cases with exotic formats. Rare in practice; when it appears the image object itself is not trustworthy.

Related errors


AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23). Data as JSON: /api/errors/2259531fd3b889e1. Report an issue: GitHub.