Intervention/image · error · ModifierException

Failed to invert image colors

Error message

Failed to invert image colors

What it means

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.

Source

Thrown at src/Drivers/Gd/Modifiers/InvertModifier.php:26

use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\InvertModifier as GenericInvertModifier;

class InvertModifier extends GenericInvertModifier implements SpecializedInterface
{
    /**
     * {@inheritdoc}
     *
     * @see ModifierInterface::apply()
     *
     * @throws ModifierException
     */
    public function apply(ImageInterface $image): ImageInterface
    {
        foreach ($image as $frame) {
            $result = imagefilter($frame->native(), IMG_FILTER_NEGATE);
            if ($result === false) {
                throw new ModifierException('Failed to invert image colors');
            }
        }

        return $image;
    }
}

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Re-decode the image from its source and apply invert() on the fresh instance
  2. Audit custom code for setNative()/imagedestroy() that corrupts frame state
  3. Convert palette frames back to truecolor before filtering
  4. Catch ModifierException and skip the effect

Example fix

// before: invert on possibly corrupted native frames
$image->invert();

// after: decode fresh and normalize before filtering
$image = $manager->decode($sourceBinary);
foreach ($image as $frame) {
    if (!imageistruecolor($frame->native())) {
        imagepalettetotruecolor($frame->native());
    }
}
$image->invert();
Defensive patterns

Strategy: try-catch

Validate before calling

foreach ($image as $frame) {
    $native = $frame->native();
    if (!$native instanceof GdImage || imagesx($native) < 1) {
        throw new RuntimeException('Frame native is not a usable GdImage');
    }
    if (!imageistruecolor($native)) {
        imagepalettetotruecolor($native);
    }
}

$image->invert();

Try / catch

use Intervention\Image\Exceptions\ModifierException;

try {
    $image->invert();
} catch (ModifierException $e) {
    $logger?->warning('Invert filter failed: ' . $e->getMessage());
}

Prevention

When it happens

Trigger: $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).

Common situations: 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.

Related errors


AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23). Data as JSON: /api/errors/34af6c17e63b14e4. Report an issue: GitHub.