{"record":{"id":"ef8bb0a083358591","repo":"Intervention/image","slug":"failed-to-apply-intervention-image-drivers-imagick-ef8bb0","errorCode":null,"errorMessage":"Failed to apply Intervention\\Image\\Drivers\\Imagick\\Modifiers\\DrawRectangleModifier, unable to build ImagickDraw object","messagePattern":"Failed to apply Intervention\\\\Image\\\\Drivers\\\\Imagick\\\\Modifiers\\\\DrawRectangleModifier, unable to build ImagickDraw object","errorType":"exception","errorClass":"ModifierException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php","lineNumber":67,"sourceCode":"        try {\n            $drawing = new ImagickDraw();\n            $drawing->setFillColor($backgroundColor);\n\n            if ($this->drawable->hasBorder()) {\n                $drawing->setStrokeColor($borderColor);\n                $drawing->setStrokeWidth($this->drawable->borderSize());\n            }\n\n            $drawing->rectangle(\n                $this->drawable->position()->x(),\n                $this->drawable->position()->y(),\n                $this->drawable->position()->x() + $this->drawable->width(),\n                $this->drawable->position()->y() + $this->drawable->height(),\n            );\n\n            return $drawing;\n        } catch (ImagickException | ImagickDrawException $e) {\n            throw new ModifierException(\n                'Failed to apply ' . self::class . ', unable to build ImagickDraw object',\n                previous: $e,\n            );\n        }\n    }\n}\n","sourceCodeStart":49,"sourceCodeEnd":74,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php#L49-L74","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Catch ModifierException and inspect $e->getPrevious() for the exact native Imagick message","Validate that width/height are non-negative finite integers and that x+width / y+height stay within reasonable bounds","Ensure border_size in the draw callback is >= 0 and the colors are valid color strings or ColorInterface instances","Raise memory_limit or reduce canvas size when drawing on very large images","Check ImageMagick policy.xml resource limits if the previous exception reports a policy/resource error","Compare behavior with the GD driver to determine whether the fault is ImageMagick-specific"],"exampleFix":"// before\n$image->drawRectangle($x, $y, $w, $h, function ($draw) {\n    $draw->background('fff')->border('f00', $border); // $border may be -1\n});\n\n// after\n$w = max(0, (int) $w);\n$h = max(0, (int) $h);\n$image->drawRectangle((int) $x, (int) $y, $w, $h, function ($draw) use ($border) {\n    $draw->background('fff')->border('f00', max(0, (int) $border));\n});","handlingStrategy":"try-catch","validationCode":"$x = (int) $x;\n$y = (int) $y;\n$width = (int) $width;\n$height = (int) $height;\nif ($width < 0 || $height < 0 || !is_finite($width) || !is_finite($height)) {\n    throw new \\InvalidArgumentException('Rectangle width/height must be non-negative integers');\n}","typeGuard":"function isValidRectangle(int $x, int $y, int $width, int $height): bool\n{\n    return $width >= 0 && $height >= 0\n        && $x <= PHP_INT_MAX - $width   // no overflow in x + width\n        && $y <= PHP_INT_MAX - $height; // no overflow in y + height\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\ModifierException;\n\ntry {\n    $image->drawRectangle($x, $y, $width, $height, $callback);\n} catch (ModifierException $e) {\n    Log::warning('Rectangle draw failed: ' . optional($e->getPrevious())->getMessage());\n    throw $e;\n}","preventionTips":["Validate width/height are non-negative before calling drawRectangle","Ensure position + dimension arithmetic cannot overflow or produce INF/NaN","Keep border sizes >= 0 and colors in valid formats","Always inspect getPrevious() for the underlying native error"],"tags":["intervention-image","imagick","imagickdraw","rectangle","drawing","php"],"backgroundTag":"imagick-draw-object-failed","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}