{"record":{"id":"c0ee18d05d991bb9","repo":"Intervention/image","slug":"failed-to-apply-intervention-image-drivers-gd-modi-c0ee18","errorCode":null,"errorMessage":"Failed to apply Intervention\\Image\\Drivers\\Gd\\Modifiers\\TrimModifier, unable to determine average color for process","messagePattern":"Failed to apply Intervention\\\\Image\\\\Drivers\\\\Gd\\\\Modifiers\\\\TrimModifier, unable to determine average color for process","errorType":"exception","errorClass":"ModifierException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Gd/Modifiers/TrimModifier.php","lineNumber":127,"sourceCode":"        $green = 0;\n        $blue = 0;\n        $alpha = 0;\n\n        // corner coordinates\n        $size = $image->size();\n        $cornerPoints = [\n            new Point(0, 0),\n            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","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Modifiers/TrimModifier.php#L109-L145","documentation":"Before trimming, the GD driver samples the four corner pixels to average a trim base color; imagecolorat() returned false for one of them. GD only returns false there when the GdImage handle is invalid or the coordinates lie outside the image, and the corner coordinates (width-1, height-1) are derived from the image itself — so the underlying GD resource is corrupted or was destroyed, not that the trim options were wrong.","triggerScenarios":"trim() on an image whose GD resource was already freed or corrupted: external code called imagedestroy() on core()->native(), a previous operation failed mid-decode and left a broken buffer, or a truncated PNG/GIF decoded to a resource with reported dimensions but unreadable pixel data.","commonSituations":"Processing truncated or bit-flipped uploads that passed a superficial MIME check; mixing raw GD calls with Intervention Image operations on the same native resource; GD instability after a prior out-of-memory condition.","solutions":["Validate the source file is fully decodable before handing it over: getimagesize() must not return false and dimensions must be >= 1.","Re-read the image from the original binary to obtain a clean GdImage and retry trim(); if it fails again, quarantine the file.","Audit any code that touches $image->core()->native() directly (imagedestroy, imagecopy onto freed handles) and remove it."],"exampleFix":"// before\n$image = $manager->read($request->file('photo')->getPathname())->trim();\n\n// after: reject files GD cannot fully decode before trimming\n$path = $request->file('photo')->getPathname();\nif (getimagesize($path) === false) {\n    throw new RuntimeException('Uploaded file is not a valid image');\n}\n$image = $manager->read($path)->trim();","handlingStrategy":"try-catch","validationCode":"if (getimagesize($path) === false) {\n    throw new RuntimeException('Uploaded file is not a valid image');\n}","typeGuard":null,"tryCatchPattern":"try { $image->trim(); } catch (ModifierException $e) { /* poisoned input: log hash, quarantine file, skip */ }","preventionTips":["Fully validate uploads (getimagesize / decode round-trip) before processing.","Do not call imagedestroy() or mutate GdImage handles you got from core()->native().","Never cache GdImage resources across requests in long-running workers."],"tags":["gd","trim","imagecolorat","corrupt-image","invalid-resource"],"backgroundTag":"corrupt-image-data","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}