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

Last frame not found in image

Error message

Last frame not found in image

What it means

Thrown by Core::last() when frame(count() - 1) cannot find the frame — wrapped as StateException. With an empty core, count() is 0 so it searches for frame(-1), which never matches; with a populated core it would only fail if the stack got corrupted between the count and the scan.

Source

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

        } catch (InvalidArgumentException $e) {
            throw new StateException('First frame not found in image', previous: $e);
        }
    }

    /**
     * {@inheritdoc}
     *
     * @see CollectableInterface::last()
     *
     * @throws DriverException
     * @throws StateException
     */
    public function last(): FrameInterface
    {
        try {
            return $this->frame($this->count() - 1);
        } catch (InvalidArgumentException $e) {
            throw new StateException('Last frame not found in image', previous: $e);
        }
    }

    /**
     * {@inheritdoc}
     *
     * @see CoreInterface::meta()
     */
    public function meta(): CollectionInterface
    {
        return $this->meta;
    }

    /**
     * {@inheritdoc}
     *
     * @see CollectionInterface::toArray()
     */

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Check count($image) > 0 before calling last()
  2. If count() reported > 0 but last() still throws, the core is corrupted — re-read the source image
  3. Avoid patterns that consume all frames then continue using the image

Example fix

// before
$frame = $image->core()->last();

// after
$count = $image->count();
$frame = $count > 0 ? $image->core()->last() : null;
if ($frame === null) {
    // handle empty animation
}
Defensive patterns

Strategy: validation

Validate before calling

$count = $image->count();
$frame = $count > 0 ? $image->core()->last() : null;
if ($frame === null) {
    // handle empty animation explicitly
}

Type guard

function hasFrames(ImageInterface $image): bool
{
    return $image->count() > 0;
}

Try / catch

use Intervention\Image\Exceptions\StateException;

try {
    $frame = $image->core()->last();
} catch (StateException $e) {
    $image = $manager->read($source);
    $frame = $image->core()->last();
}

Prevention

When it happens

Trigger: Calling $image->core()->last() on a core with zero frames (after clear(), an all-removing slice(), or manual construction without add()), or on a stack whose frames were invalidated mid-operation.

Common situations: Same empty-core situations as first(); batch pipelines that drain frames one by one and then ask for last(); assertions in tests that empty an animation and still call last().

Related errors


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