{"record":{"id":"1852b4e3c52bd362","repo":"Intervention/image","slug":"failed-to-apply-intervention-image-drivers-gd-modi-1852b4","errorCode":null,"errorMessage":"Failed to apply Intervention\\Image\\Drivers\\Gd\\Modifiers\\TrimModifier, unable to read trim color from index","messagePattern":"Failed to apply Intervention\\\\Image\\\\Drivers\\\\Gd\\\\Modifiers\\\\TrimModifier, unable to read trim color from index","errorType":"exception","errorClass":"ModifierException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Modifiers/TrimModifier.php","lineNumber":135,"sourceCode":"            new Point($size->width() - 1, 0),\n            new Point(0, $size->height() - 1),\n            new Point($size->width() - 1, $size->height() - 1),\n        ];\n\n        // create an average color to be used in trim operation\n        foreach ($cornerPoints as $pos) {\n            $cornerColor = imagecolorat($image->core()->native(), $pos->x(), $pos->y());\n\n            if ($cornerColor === false) {\n                throw new ModifierException(\n                    'Failed to apply ' . self::class . ', unable to determine average color for process',\n                );\n            }\n\n            try {\n                $rgb = imagecolorsforindex($image->core()->native(), $cornerColor);\n            } catch (ValueError) {\n                throw new ModifierException(\n                    '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) {","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Modifiers/TrimModifier.php#L117-L153","documentation":"After imagecolorat() returns a color index for a corner pixel, GD's imagecolorsforindex() must translate it into RGBA parts; it raised a ValueError because that index does not exist in the image's color table. This is the signature of a malformed palette image — a PNG or GIF whose palette holds fewer entries than the index stored in its pixel data — since a well-formed image never references an unallocated palette slot.","triggerScenarios":"trim() on a palette-based PNG or GIF whose PLTE chunk was truncated, hand-edited, or written by a broken encoder; files that survived a partial upload with intact headers but damaged palette data.","commonSituations":"User uploads delivered truncated (network cut, wrong Content-Length); images produced by obscure third-party exporter libraries; GIFs re-assembled from frame dumps.","solutions":["Re-encode the suspect source once through a strict decoder to normalize the palette, then trim the re-encoded data.","Validate uploads with an imagecreatefromstring() round-trip and reject files that fail.","Convert palette images to truecolor with imagepalettetotruecolor() before trimming so no palette lookup is needed."],"exampleFix":"// before\n$image = $manager->read('broken-palette.gif')->trim();\n\n// after: normalize a suspect palette image to truecolor PNG, then trim\n$gd = imagecreatefromstring(file_get_contents('broken-palette.gif'));\nimagepalettetotruecolor($gd);\nob_start();\nimagepng($gd);\n$image = $manager->read(ob_get_clean())->trim();","handlingStrategy":"try-catch","validationCode":"// round-trip decode to reject files with corrupt palettes\n$gd = @imagecreatefromstring(file_get_contents($path));\nif ($gd === false) {\n    throw new RuntimeException('Corrupt or truncated image file');\n}","typeGuard":null,"tryCatchPattern":"try { $image->trim(); } catch (ModifierException $e) { /* treat as corrupt source: re-encode from binary once, then quarantine */ }","preventionTips":["Normalize palette PNG/GIF uploads to truecolor at ingest.","Reject uploads whose size differs from Content-Length or that fail decode round-trips.","Re-encode files from untrusted origins through a strict decoder before trimming."],"tags":["gd","trim","palette","imagecolorsforindex","corrupt-image"],"backgroundTag":"corrupt-image-data","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}