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

Failed to count image frames

Error message

Failed to count image frames

What it means

Thrown by Core::count() when Imagick::getNumberImages() raises an ImagickException. The core should always be able to report its frame count, so this error signals that the underlying Imagick object is destroyed, cleared, or in a corrupted internal state.

Source

Thrown at src/Drivers/Imagick/Core.php:227

            throw new DriverException('Failed to add image frame', previous: $e);
        }

        return $this;
    }

    /**
     * {@inheritdoc}
     *
     * @see CoreInterface::count()
     *
     * @throws DriverException
     */
    public function count(): int
    {
        try {
            return $this->imagick->getNumberImages();
        } catch (ImagickException $e) {
            throw new DriverException('Failed to count image frames', previous: $e);
        }
    }

    /**
     * {@inheritdoc}
     *
     * @see Iterator::rewind()
     *
     * @throws DriverException
     */
    public function current(): mixed
    {
        try {
            $this->imagick->setIteratorIndex($this->iteratorIndex);

            return new Frame($this->imagick->current());
        } catch (ImagickException | RuntimeException $e) {
            throw new DriverException('Failed to iterate image frames', previous: $e);

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Read the previous ImagickException to confirm resource-destruction vs. corruption
  2. Do not reuse an Image whose core was mutated when an earlier operation threw; re-read the source
  3. Avoid calling $image->core()->clear() manually unless you discard the image afterwards
  4. Keep the ImageManager->read() and the operations in the same scope so PHP does not garbage-collect the native object early
Defensive patterns

Strategy: try-catch

Try / catch

use Intervention\Image\Exceptions\DriverException;

try {
    $frames = $image->count();
} catch (DriverException $e) {
    // core is unusable (destroyed/cleared native object)
    $image = $manager->read($sourcePath);
    $frames = $image->count();
}

Prevention

When it happens

Trigger: Calling $image->count(), or any API that internally counts frames (Core::last(), RemoveAnimationModifier::normalizePosition() with a '50%' string position), after the native Imagick object was destructed, cleared via clear(), or invalidated by a failed operation that left the core half-modified.

Common situations: Holding an Image across long-running jobs while its Imagick resource was freed; reusing a core after an exception in slice()/add(); reference bugs where the Imagick object was replaced by a destroyed handle; imagick extension crashes mid-operation.

Related errors


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