Intervention/image · error · ModifierException
Failed to apply Intervention\Image\Drivers\Gd\Modifiers\Trim
Error message
Failed to apply Intervention\Image\Drivers\Gd\Modifiers\TrimModifier, unable to allocate trim color
What it means
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.
Source
Thrown at src/Drivers/Gd/Modifiers/TrimModifier.php:154
'Failed to apply ' . self::class . ', unable to read trim color from index',
);
}
$red += round(round($rgb['red'] / 51) * 51);
$green += round(round($rgb['green'] / 51) * 51);
$blue += round(round($rgb['blue'] / 51) * 51);
$alpha += $rgb['alpha'];
}
$red = (int) round($red / 4);
$green = (int) round($green / 4);
$blue = (int) round($blue / 4);
$alpha = (int) round($alpha / 4);
$color = imagecolorallocatealpha($image->core()->native(), $red, $green, $blue, $alpha);
if ($color === false) {
throw new ModifierException(
'Failed to apply ' . self::class . ', unable to allocate trim color',
);
}
return $color;
}
}
View on GitHub (pinned to 5598b9e397)
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.
Example fix
// before
$image = $manager->read('indexed.png')->trim(); // 256-slot palette is full
// after: free the palette constraint by converting to truecolor first
imagepalettetotruecolor($image->core()->native());
$image->trim(); Defensive patterns
Strategy: validation
Validate before calling
// palette images with a full 256-slot table cannot allocate the trim color
$native = $image->core()->native();
if (!imageistruecolor($native)) {
imagepalettetotruecolor($native);
}
$image->trim(); Try / catch
try { $image->trim(); } catch (ModifierException $e) { imagepalettetotruecolor($image->core()->native()); $image->trim(); } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Trimming optimized/indexed web assets (PNG-8, GIF) or exports from tools that quantize to palette; images never converted to truecolor at ingest.
Related errors
- Failed to apply Intervention\Image\Drivers\Gd\Modifiers\Trim
- The specified index is outside of the range
- Failed to apply Intervention\Image\Drivers\Gd\Modifiers\Shar
- Trim modifier cannot be applied to animated images
- Failed to apply Intervention\Image\Drivers\Gd\Modifiers\Trim
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/511cdfbebb3cf55e.
Report an issue: GitHub.