{"record":{"id":"511cdfbebb3cf55e","repo":"Intervention/image","slug":"failed-to-apply-intervention-image-drivers-gd-modi-511cdf","errorCode":null,"errorMessage":"Failed to apply Intervention\\Image\\Drivers\\Gd\\Modifiers\\TrimModifier, unable to allocate trim color","messagePattern":"Failed to apply Intervention\\\\Image\\\\Drivers\\\\Gd\\\\Modifiers\\\\TrimModifier, unable to allocate trim color","errorType":"exception","errorClass":"ModifierException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Modifiers/TrimModifier.php","lineNumber":154,"sourceCode":"                    'Failed to apply ' . self::class . ', unable to read trim color from index',\n                );\n            }\n\n            $red += round(round($rgb['red'] / 51) * 51);\n            $green += round(round($rgb['green'] / 51) * 51);\n            $blue += round(round($rgb['blue'] / 51) * 51);\n            $alpha += $rgb['alpha'];\n        }\n\n        $red = (int) round($red / 4);\n        $green = (int) round($green / 4);\n        $blue = (int) round($blue / 4);\n        $alpha = (int) round($alpha / 4);\n\n        $color = imagecolorallocatealpha($image->core()->native(), $red, $green, $blue, $alpha);\n\n        if ($color === false) {\n            throw new ModifierException(\n                'Failed to apply ' . self::class . ', unable to allocate trim color',\n            );\n        }\n\n        return $color;\n    }\n}\n","sourceCodeStart":136,"sourceCodeEnd":162,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Modifiers/TrimModifier.php#L136-L162","documentation":"The averaged corner color must be registered in the source image via imagecolorallocatealpha() before GD's imagecropauto() can use it as the trim reference. Allocation returned false: on truecolor images this is effectively unreachable for in-range RGBA values, but on palette images (created in the imagecreate() style, e.g. decoded indexed PNG/GIF) it fails when all 256 palette slots are already occupied and no slot is free for the trim color.","triggerScenarios":"trim() on an indexed/palette image whose 256-color palette is full; palette PNGs or GIFs fed to the GD driver without prior truecolor conversion.","commonSituations":"Trimming optimized/indexed web assets (PNG-8, GIF) or exports from tools that quantize to palette; images never converted to truecolor at ingest.","solutions":["Convert the image to truecolor before trimming: imagepalettetotruecolor($image->core()->native());","Normalize uploads to truecolor PNG/JPEG at ingest time so the pipeline never sees full palettes.","Run the trim on the Imagick driver, which has no palette slot limit."],"exampleFix":"// before\n$image = $manager->read('indexed.png')->trim(); // 256-slot palette is full\n\n// after: free the palette constraint by converting to truecolor first\nimagepalettetotruecolor($image->core()->native());\n$image->trim();","handlingStrategy":"validation","validationCode":"// palette images with a full 256-slot table cannot allocate the trim color\n$native = $image->core()->native();\nif (!imageistruecolor($native)) {\n    imagepalettetotruecolor($native);\n}\n$image->trim();","typeGuard":null,"tryCatchPattern":"try { $image->trim(); } catch (ModifierException $e) { imagepalettetotruecolor($image->core()->native()); $image->trim(); }","preventionTips":["Convert indexed uploads to truecolor at ingest time.","Check imageistruecolor() before trim() when sources may be palette-based.","Remember trim() allocates a color in the SOURCE image, not in a fresh canvas."],"tags":["gd","trim","palette","imagecolorallocatealpha"],"backgroundTag":"gd-palette-full","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}