{"record":{"id":"f383caebee100d2d","repo":"Intervention/image","slug":"failed-to-get-image-loop-count","errorCode":null,"errorMessage":"Failed to get image loop count","messagePattern":"Failed to get image loop count","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\DriverException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Core.php","lineNumber":359,"sourceCode":"            }\n        }\n\n        throw new InvalidArgumentException('Frame #' . $position . ' could not be found in the image');\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see CoreInterface::loops()\n     *\n     * @throws DriverException\n     */\n    public function loops(): int\n    {\n        try {\n            return $this->imagick->getImageIterations();\n        } catch (ImagickException $e) {\n            throw new DriverException('Failed to get image loop count', previous: $e);\n        }\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see CoreInterface::setLoops()\n     *\n     * @throws DriverException\n     */\n    public function setLoops(int $loops): CoreInterface\n    {\n        try {\n            $this->imagick->resetIterator();\n            $this->imagick->setImageIterations($loops);\n        } catch (ImagickException $e) {\n            throw new DriverException('Failed to set image loop count', previous: $e);\n        }","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Core.php#L341-L377","documentation":"Thrown by Core::loops() when Imagick::getImageIterations() fails with an ImagickException. getImageIterations reads the GIF/WebP loop count; failing here means the native object cannot answer the question at all — destroyed, cleared, or holding frames without animation metadata in an unreadable state.","triggerScenarios":"Calling $image->loops() on the Imagick driver after the core's Imagick object was cleared/destroyed, or on a core assembled from frames whose container state is invalid (e.g. after a failed slice or add left a half-built stack).","commonSituations":"Reading loop counts after destructive operations threw earlier; images held across requests in long-lived workers while the resource was freed; cores built manually frame-by-frame without a coalesced base.","solutions":["Call loops() on an image straight from $manager->read(); if that works, your earlier mutation corrupted the core","Re-read the source instead of continuing after any exception from slice/add","Check $e->getPrevious() for the native reason (usually 'unable to read iterations' style messages)"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"use Intervention\\Image\\Exceptions\\DriverException;\n\ntry {\n    $loops = $image->loops();\n} catch (DriverException $e) {\n    $loops = 0; // sensible default for a broken/unreadable animation state\n}","preventionTips":["Read loop counts right after read(), before mutations","Treat loops() failures as a signal the core is corrupted — re-read the source","Default to 0 in consumers so a metadata hiccup does not crash rendering"],"tags":["imagick","animation","gif","loops","metadata"],"backgroundTag":"animation-loop-count-read-failed","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}