{"record":{"id":"34af6c17e63b14e4","repo":"Intervention/image","slug":"failed-to-invert-image-colors","errorCode":null,"errorMessage":"Failed to invert image colors","messagePattern":"Failed to invert image colors","errorType":"exception","errorClass":"ModifierException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Modifiers/InvertModifier.php","lineNumber":26,"sourceCode":"use Intervention\\Image\\Interfaces\\ImageInterface;\nuse Intervention\\Image\\Interfaces\\SpecializedInterface;\nuse Intervention\\Image\\Modifiers\\InvertModifier as GenericInvertModifier;\n\nclass InvertModifier extends GenericInvertModifier implements SpecializedInterface\n{\n    /**\n     * {@inheritdoc}\n     *\n     * @see ModifierInterface::apply()\n     *\n     * @throws ModifierException\n     */\n    public function apply(ImageInterface $image): ImageInterface\n    {\n        foreach ($image as $frame) {\n            $result = imagefilter($frame->native(), IMG_FILTER_NEGATE);\n            if ($result === false) {\n                throw new ModifierException('Failed to invert image colors');\n            }\n        }\n\n        return $image;\n    }\n}\n","sourceCodeStart":8,"sourceCodeEnd":33,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Modifiers/InvertModifier.php#L8-L33","documentation":"The GD driver inverts colors with imagefilter($gd, IMG_FILTER_NEGATE) per frame and throws ModifierException when the call returns false. Driver-decoded frames are truecolor GdImage resources on which negation succeeds, so a false return means the native image is unusable (invalidated or destroyed resource) or was converted to a state GD refuses to filter. The call has no parameters that could be wrong.","triggerScenarios":"$image->invert() after custom code invalidated frame natives (setNative() with a broken GdImage, manual imagedestroy()); filtering palette frames from hand-built cores; the library's test suite forcing imagefilter() to fail (testApplyThrowsWhenSpecializedWithoutOverride).","commonSituations":"Workers that free raw GD resources while image objects survive; interop layers mixing resource-level GD code with Intervention objects; test doubles exercising the failure branch.","solutions":["Re-decode the image from its source and apply invert() on the fresh instance","Audit custom code for setNative()/imagedestroy() that corrupts frame state","Convert palette frames back to truecolor before filtering","Catch ModifierException and skip the effect"],"exampleFix":"// before: invert on possibly corrupted native frames\n$image->invert();\n\n// after: decode fresh and normalize before filtering\n$image = $manager->decode($sourceBinary);\nforeach ($image as $frame) {\n    if (!imageistruecolor($frame->native())) {\n        imagepalettetotruecolor($frame->native());\n    }\n}\n$image->invert();","handlingStrategy":"try-catch","validationCode":"foreach ($image as $frame) {\n    $native = $frame->native();\n    if (!$native instanceof GdImage || imagesx($native) < 1) {\n        throw new RuntimeException('Frame native is not a usable GdImage');\n    }\n    if (!imageistruecolor($native)) {\n        imagepalettetotruecolor($native);\n    }\n}\n\n$image->invert();","typeGuard":null,"tryCatchPattern":"use Intervention\\Image\\Exceptions\\ModifierException;\n\ntry {\n    $image->invert();\n} catch (ModifierException $e) {\n    $logger?->warning('Invert filter failed: ' . $e->getMessage());\n}","preventionTips":["Do not destroy or replace native GD resources while image objects are in use.","Process images in short-lived instances rather than caching them across long jobs.","Normalize palette frames to truecolor before GD filters."],"tags":["gd","invert","imagefilter","filter-failure","native-resource"],"backgroundTag":"gd-imagefilter-failed","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}