Intervention/image · error · ModifierException

Failed to apply Intervention\Image\Drivers\Imagick\Modifiers

Error message

Failed to apply Intervention\Image\Drivers\Imagick\Modifiers\DrawRectangleModifier, unable to build ImagickDraw object

What it means

This ModifierException is thrown by the Imagick driver when $image->drawRectangle() fails to build the native ImagickDraw object. The private rectangle() method in src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php:47 wraps ImagickException and ImagickDrawException raised while setting the fill/stroke colors or the rectangle coordinates (position + width/height). The native failure is attached as the previous exception for diagnosis.

Source

Thrown at src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php:67

        try {
            $drawing = new ImagickDraw();
            $drawing->setFillColor($backgroundColor);

            if ($this->drawable->hasBorder()) {
                $drawing->setStrokeColor($borderColor);
                $drawing->setStrokeWidth($this->drawable->borderSize());
            }

            $drawing->rectangle(
                $this->drawable->position()->x(),
                $this->drawable->position()->y(),
                $this->drawable->position()->x() + $this->drawable->width(),
                $this->drawable->position()->y() + $this->drawable->height(),
            );

            return $drawing;
        } catch (ImagickException | ImagickDrawException $e) {
            throw new ModifierException(
                'Failed to apply ' . self::class . ', unable to build ImagickDraw object',
                previous: $e,
            );
        }
    }
}

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Catch ModifierException and inspect $e->getPrevious() for the exact native Imagick message
  2. Validate that width/height are non-negative finite integers and that x+width / y+height stay within reasonable bounds
  3. Ensure border_size in the draw callback is >= 0 and the colors are valid color strings or ColorInterface instances
  4. Raise memory_limit or reduce canvas size when drawing on very large images
  5. Check ImageMagick policy.xml resource limits if the previous exception reports a policy/resource error
  6. Compare behavior with the GD driver to determine whether the fault is ImageMagick-specific

Example fix

// before
$image->drawRectangle($x, $y, $w, $h, function ($draw) {
    $draw->background('fff')->border('f00', $border); // $border may be -1
});

// after
$w = max(0, (int) $w);
$h = max(0, (int) $h);
$image->drawRectangle((int) $x, (int) $y, $w, $h, function ($draw) use ($border) {
    $draw->background('fff')->border('f00', max(0, (int) $border));
});
Defensive patterns

Strategy: try-catch

Validate before calling

$x = (int) $x;
$y = (int) $y;
$width = (int) $width;
$height = (int) $height;
if ($width < 0 || $height < 0 || !is_finite($width) || !is_finite($height)) {
    throw new \InvalidArgumentException('Rectangle width/height must be non-negative integers');
}

Type guard

function isValidRectangle(int $x, int $y, int $width, int $height): bool
{
    return $width >= 0 && $height >= 0
        && $x <= PHP_INT_MAX - $width   // no overflow in x + width
        && $y <= PHP_INT_MAX - $height; // no overflow in y + height
}

Try / catch

use Intervention\Image\Exceptions\ModifierException;

try {
    $image->drawRectangle($x, $y, $width, $height, $callback);
} catch (ModifierException $e) {
    Log::warning('Rectangle draw failed: ' . optional($e->getPrevious())->getMessage());
    throw $e;
}

Prevention

When it happens

Trigger: Calling $image->drawRectangle($x, $y, $width, $height, $callback) with coordinates derived from NAN/INF values; a width or height that is negative or overflows when added to the position (x + width exceeding PHP int range); border options with a negative border_size; an unparseable background/border color; insufficient memory to allocate ImagickDraw.

Common situations: Rectangle dimensions computed from unvalidated form input; drawing rectangles sized from aspect-ratio math that produces INF or negative values; low memory_limit hosting; ImageMagick installations whose policy.xml caps image area or memory; broken color strings like 'zzz' that fail during pixel export.

Related errors


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