Intervention/image · error · Intervention\Image\Exceptions\DriverException
Failed to slice image
Error message
Failed to slice image
What it means
Core::slice($offset, $length) deletes every frame outside the window [$offset, $offset+$length) and then rebuilds the list via coalesceImages(). ImagickException during that surgery — most often because the window covers zero frames (offset beyond the frame count, or length 0), which removes every image and leaves getImageIterations()/coalesceImages() running against an empty Imagick — is wrapped as this DriverException.
Source
Thrown at src/Drivers/Imagick/Core.php:163
/**
* {@inheritdoc}
*
* @see CollectionInterface::slice()
*
* @throws DriverException
*/
public function slice(int $offset, ?int $length = null): CollectionInterface
{
$length = is_null($length) ? $this->count() : $length;
$count = $this->count();
for ($i = $count - 1; $i >= 0; $i--) {
if ($i < $offset || $i >= $offset + $length) {
try {
$this->imagick->setIteratorIndex($i);
$this->imagick->removeImage();
} catch (ImagickException $e) {
throw new DriverException('Failed to slice image', previous: $e);
}
}
}
try {
$loops = $this->imagick->getImageIterations();
$coalesced = $this->imagick->coalesceImages();
$this->imagick->clear();
$this->imagick = $coalesced;
$this->imagick->setImageIterations($loops);
} catch (ImagickException $e) {
throw new DriverException('Failed to slice image', previous: $e);
}
return $this;
}
/**View on GitHub (pinned to 5598b9e397)
Solutions
- Clamp arguments against the real frame count before slicing: offset into [0, count-1] and length into [1, count-offset].
- Validate the requested window against $image->core()->count() and reject nonsensical ranges upstream.
- For huge GIFs, raise memory_limit or reduce frame count before slicing so coalesceImages() can complete.
Example fix
// before $image->core()->slice(5, 3); // gif only has 4 frames → all frames removed → Imagick error // after $count = $image->core()->count(); $offset = max(0, min(5, $count - 1)); $length = max(1, min(3, $count - $offset)); $image->core()->slice($offset, $length);
Defensive patterns
Strategy: validation
Validate before calling
$count = $image->core()->count(); $offset = max(0, min($offset, $count - 1)); $length = max(1, min($length, $count - $offset)); $image->core()->slice($offset, $length);
Try / catch
try { $image->core()->slice($offset, $length); } catch (DriverException $e) { /* usually a degenerate window: validate against count() and retry with clamped values */ } Prevention
- Always clamp offset/length against the live frame count before slicing.
- Validate animation-trimming parameters from user input against count().
- Remember a window covering zero frames empties the Imagick container and fails.
When it happens
Trigger: slice($offset, $length) with offset >= frame count or length <= 0; slicing a single-frame image to nothing; very large GIFs where coalesceImages() exhausts memory mid-rebuild.
Common situations: Animation trimming code computing offset/length from unvalidated user input; GIF batch pipelines; slicing after frame removal shifted the count.
Related errors
- Failed to get current frame data
- Given color space must implement Intervention\Image\Interfac
- Failed to apply {class}, unable to re-apply image frame
- Offset is not in the range of frames
- Frame #${position} could not be found in the image
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/1a4842f0e3cbf082.
Report an issue: GitHub.