{"record":{"id":"1f7345c5de048cb4","repo":"Intervention/image","slug":"failed-to-count-image-frames","errorCode":null,"errorMessage":"Failed to count image frames","messagePattern":"Failed to count image frames","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\DriverException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Core.php","lineNumber":227,"sourceCode":"            throw new DriverException('Failed to add image frame', previous: $e);\n        }\n\n        return $this;\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see CoreInterface::count()\n     *\n     * @throws DriverException\n     */\n    public function count(): int\n    {\n        try {\n            return $this->imagick->getNumberImages();\n        } catch (ImagickException $e) {\n            throw new DriverException('Failed to count image frames', previous: $e);\n        }\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see Iterator::rewind()\n     *\n     * @throws DriverException\n     */\n    public function current(): mixed\n    {\n        try {\n            $this->imagick->setIteratorIndex($this->iteratorIndex);\n\n            return new Frame($this->imagick->current());\n        } catch (ImagickException | RuntimeException $e) {\n            throw new DriverException('Failed to iterate image frames', previous: $e);","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Core.php#L209-L245","documentation":"Thrown by Core::count() when Imagick::getNumberImages() raises an ImagickException. The core should always be able to report its frame count, so this error signals that the underlying Imagick object is destroyed, cleared, or in a corrupted internal state.","triggerScenarios":"Calling $image->count(), or any API that internally counts frames (Core::last(), RemoveAnimationModifier::normalizePosition() with a '50%' string position), after the native Imagick object was destructed, cleared via clear(), or invalidated by a failed operation that left the core half-modified.","commonSituations":"Holding an Image across long-running jobs while its Imagick resource was freed; reusing a core after an exception in slice()/add(); reference bugs where the Imagick object was replaced by a destroyed handle; imagick extension crashes mid-operation.","solutions":["Read the previous ImagickException to confirm resource-destruction vs. corruption","Do not reuse an Image whose core was mutated when an earlier operation threw; re-read the source","Avoid calling $image->core()->clear() manually unless you discard the image afterwards","Keep the ImageManager->read() and the operations in the same scope so PHP does not garbage-collect the native object early"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"use Intervention\\Image\\Exceptions\\DriverException;\n\ntry {\n    $frames = $image->count();\n} catch (DriverException $e) {\n    // core is unusable (destroyed/cleared native object)\n    $image = $manager->read($sourcePath);\n    $frames = $image->count();\n}","preventionTips":["Do not call $image->core()->clear() on images you keep using","After any exception from core mutations, re-read the source instead of continuing","Perform read + operate + encode within one scope so the native object outlives its usage"],"tags":["imagick","frames","state","count"],"backgroundTag":"imagick-object-invalid-state","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}