{"record":{"id":"1a4842f0e3cbf082","repo":"Intervention/image","slug":"failed-to-slice-image","errorCode":null,"errorMessage":"Failed to slice image","messagePattern":"Failed to slice image","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\DriverException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Core.php","lineNumber":163,"sourceCode":"    /**\n     * {@inheritdoc}\n     *\n     * @see CollectionInterface::slice()\n     *\n     * @throws DriverException\n     */\n    public function slice(int $offset, ?int $length = null): CollectionInterface\n    {\n        $length = is_null($length) ? $this->count() : $length;\n        $count = $this->count();\n\n        for ($i = $count - 1; $i >= 0; $i--) {\n            if ($i < $offset || $i >= $offset + $length) {\n                try {\n                    $this->imagick->setIteratorIndex($i);\n                    $this->imagick->removeImage();\n                } catch (ImagickException $e) {\n                    throw new DriverException('Failed to slice image', previous: $e);\n                }\n            }\n        }\n\n        try {\n            $loops = $this->imagick->getImageIterations();\n            $coalesced = $this->imagick->coalesceImages();\n            $this->imagick->clear();\n            $this->imagick = $coalesced;\n            $this->imagick->setImageIterations($loops);\n        } catch (ImagickException $e) {\n            throw new DriverException('Failed to slice image', previous: $e);\n        }\n\n        return $this;\n    }\n\n    /**","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Core.php#L145-L181","documentation":"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.","triggerScenarios":"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.","commonSituations":"Animation trimming code computing offset/length from unvalidated user input; GIF batch pipelines; slicing after frame removal shifted the count.","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."],"exampleFix":"// before\n$image->core()->slice(5, 3); // gif only has 4 frames → all frames removed → Imagick error\n\n// after\n$count = $image->core()->count();\n$offset = max(0, min(5, $count - 1));\n$length = max(1, min(3, $count - $offset));\n$image->core()->slice($offset, $length);","handlingStrategy":"validation","validationCode":"$count = $image->core()->count();\n$offset = max(0, min($offset, $count - 1));\n$length = max(1, min($length, $count - $offset));\n$image->core()->slice($offset, $length);","typeGuard":null,"tryCatchPattern":"try { $image->core()->slice($offset, $length); } catch (DriverException $e) { /* usually a degenerate window: validate against count() and retry with clamped values */ }","preventionTips":["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."],"tags":["imagick","core","slice","frames","gif","bounds"],"backgroundTag":"argument-out-of-range","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}