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

Offset is not in the range of frames

Error message

Offset is not in the range of frames

What it means

InvalidArgumentException thrown by SliceAnimationModifier before any frame is removed: the offset must reference an existing frame, i.e. $offset < $image->count(). Offsets are 0-based frame indexes and the check runs in the Imagick-specialized apply() before core()->slice() executes, so nothing is modified when it fires.

Source

Thrown at src/Drivers/Imagick/Modifiers/SliceAnimationModifier.php:20

declare(strict_types=1);

namespace Intervention\Image\Drivers\Imagick\Modifiers;

use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\SliceAnimationModifier as GenericSliceAnimationModifier;

class SliceAnimationModifier extends GenericSliceAnimationModifier implements SpecializedInterface
{
    /**
     * @throws InvalidArgumentException
     */
    public function apply(ImageInterface $image): ImageInterface
    {
        if ($this->offset >= $image->count()) {
            throw new InvalidArgumentException('Offset is not in the range of frames');
        }

        $image->core()->slice($this->offset, $this->length);

        return $image;
    }
}

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Compare offset against $image->count() before calling and clamp or reject
  2. Derive the offset from the actual frame count: $offset = min($offset, $image->count() - 1)
  3. For 'last N frames' semantics compute offset as max(0, $image->count() - $length)
  4. Guard with $image->isAnimated() (or count() > 1) before slicing static images

Example fix

// before
$image->sliceAnimation(8, 3); // 5-frame GIF -> InvalidArgumentException

// after
$offset = min(8, $image->count() - 1);
$image->sliceAnimation($offset, 3);
Defensive patterns

Strategy: validation

Validate before calling

if ($offset < 0 || $offset >= $image->count()) {
    throw new InvalidArgumentException(sprintf(
        'Slice offset %d is outside 0..%d for this image',
        $offset,
        max(0, $image->count() - 1)
    ));
}
$image->sliceAnimation($offset, $length);

Type guard

function isValidSliceOffset(int $offset, ImageInterface $image): bool
{
    return $offset >= 0 && $offset < $image->count();
}

Prevention

When it happens

Trigger: $image->sliceAnimation($offset, $length) with $offset >= frame count: sliceAnimation(5) on a 5-frame GIF (valid indexes are 0-4), any offset >= 1 on a static single-frame image (count() === 1), or slicing again after an earlier sliceAnimation() already reduced the frame count.

Common situations: Hard-coded frame offsets after switching to source files with fewer frames; computing offset from user input or metadata without bounds-checking; calling sliceAnimation on non-animated images; chaining slices without accounting for the shrinking stack.

Related errors


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