Intervention/image · error · InvalidArgumentException

Length must be greater than or equal to 1

Error message

Length must be greater than or equal to 1

What it means

SliceAnimationModifier backs $image->sliceAnimation($offset, $length) on animated images. offset selects the frame to start from (0-based, no lower bound enforced here) and length is how many frames to keep, where null means 'until the end of the animation'. Because keeping zero or a negative number of frames is meaningless, the constructor throws InvalidArgumentException when length is a non-null int <= 0.

Source

Thrown at src/Modifiers/SliceAnimationModifier.php:18

<?php

declare(strict_types=1);

namespace Intervention\Image\Modifiers;

use Intervention\Image\Drivers\SpecializableModifier;
use Intervention\Image\Exceptions\InvalidArgumentException;

class SliceAnimationModifier extends SpecializableModifier
{
    /**
     * @throws InvalidArgumentException
     */
    public function __construct(public int $offset = 0, public ?int $length = null)
    {
        if ($this->length !== null && $this->length <= 0) {
            throw new InvalidArgumentException('Length must be greater than or equal to 1');
        }
    }
}

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Pass null for length when you want all remaining frames: $image->sliceAnimation(10)
  2. Guard computed lengths: $length = $length > 0 ? $length : null
  3. Validate user input for length as a positive integer before calling

Example fix

// before
$image->sliceAnimation($offset, $framesLeft); // throws when $framesLeft is 0

// after
$image->sliceAnimation($offset, $framesLeft > 0 ? $framesLeft : null);
Defensive patterns

Strategy: validation

Validate before calling

// Normalize length: null means 'all remaining frames'
$length = isset($input['length']) ? (int) $input['length'] : null;
$length = ($length !== null && $length > 0) ? $length : null;

$image->sliceAnimation($offset, $length);

Type guard

function isValidSliceLength(mixed $length): bool
{
    return $length === null || (is_int($length) && $length >= 1);
}

Try / catch

use Intervention\Image\Exceptions\InvalidArgumentException;

try {
    $image->sliceAnimation($offset, $length);
} catch (InvalidArgumentException $e) {
    // message: 'Length must be greater than or equal to 1'
    $image->sliceAnimation($offset, null); // keep all remaining frames
}

Prevention

When it happens

Trigger: Calling $image->sliceAnimation(0, 0), $image->sliceAnimation(5, -3), or new SliceAnimationModifier(offset: 0, length: 0). Length computed as e.g. $frameCount - $skip that ends up 0 or negative is a typical source.

Common situations: Frame-range math on animated GIFs/WebPs where the requested range is empty (offset beyond frame count making a computed length 0); passing a literal 0 intending 'no limit' instead of null; user-supplied frame counts that arrive as 0.

Related errors


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