{"record":{"id":"7c5720bbacd93cee","repo":"Intervention/image","slug":"trim-modifier-cannot-be-applied-to-animated-images-7c5720","errorCode":null,"errorMessage":"Trim modifier cannot be applied to animated images","messagePattern":"Trim modifier cannot be applied to animated images","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Modifiers/TrimModifier.php","lineNumber":23,"sourceCode":"namespace Intervention\\Image\\Drivers\\Imagick\\Modifiers;\n\nuse ImagickException;\nuse Intervention\\Image\\Exceptions\\ModifierException;\nuse Intervention\\Image\\Exceptions\\NotSupportedException;\nuse Intervention\\Image\\Interfaces\\ImageInterface;\nuse Intervention\\Image\\Interfaces\\SpecializedInterface;\nuse Intervention\\Image\\Modifiers\\TrimModifier as GenericTrimModifier;\n\nclass TrimModifier extends GenericTrimModifier implements SpecializedInterface\n{\n    /**\n     * @throws NotSupportedException\n     * @throws ModifierException\n     */\n    public function apply(ImageInterface $image): ImageInterface\n    {\n        if ($image->isAnimated()) {\n            throw new NotSupportedException('Trim modifier cannot be applied to animated images');\n        }\n\n        $imagick = $image->core()->native();\n\n        try {\n            $result = $imagick->trimImage(($this->tolerance / 100 * $imagick->getQuantum()) / 1.5)\n                && $imagick->setImagePage(0, 0, 0, 0);\n\n            if ($result === false) {\n                throw new ModifierException(\n                    'Failed to apply ' . self::class . ', unable to processs image trimming',\n                );\n            }\n        } catch (ImagickException $e) {\n            throw new ModifierException(\n                'Failed to apply ' . self::class . ', unable to processs image trimming',\n                previous: $e,\n            );","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Modifiers/TrimModifier.php#L5-L41","documentation":"NotSupportedException thrown by the Imagick TrimModifier before any native call: trim is only supported on static images. Trimming an animation would trim each frame against its own background and desync frame geometry, so the modifier fails fast whenever $image->isAnimated() is true. The GD driver throws the identical error.","triggerScenarios":"$image->trim($tolerance) on a multi-frame image: animated GIF, animated WebP, or APNG loaded with animation decoding enabled (decodeAnimation defaults to true) on the Imagick driver.","commonSituations":"Running a generic 'trim borders' pipeline over mixed uploads that include animated GIFs; trimming user avatars or stickers that turn out to be animated; processing animation formats with a pipeline written for photos.","solutions":["Check $image->isAnimated() before trimming and branch the pipeline","Reduce to a single frame first: $image = $image->sliceAnimation(0, 1); then trim","Read the file with decodeAnimation: false in the driver config when only a static result is needed","For animated sources, trim one representative frame to compute the crop, then apply crop() to the whole animation instead"],"exampleFix":"// before\n$image->trim(); // animated GIF -> NotSupportedException\n\n// after\nif ($image->isAnimated()) {\n    $image = $image->sliceAnimation(0, 1); // first frame only\n}\n$image->trim();","handlingStrategy":"validation","validationCode":"if ($image->isAnimated()) {\n    $image = $image->sliceAnimation(0, 1); // reduce to first frame\n}\n$image->trim($tolerance);","typeGuard":"function isTrimmable(ImageInterface $image): bool\n{\n    return !$image->isAnimated();\n}","tryCatchPattern":null,"preventionTips":["Set decodeAnimation: false in the driver config when animated output is never needed","Branch upload pipelines on isAnimated() instead of assuming static images","Write tests with an animated GIF fixture for any pipeline that trims"],"tags":["imagick","trim","animation","gif","not-supported"],"backgroundTag":"animated-image-not-supported","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}