Intervention/image · error · ModifierException

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

Error message

Failed to apply Intervention\Image\Drivers\Imagick\Modifiers\ContainModifier, unable to resize image

What it means

Thrown by ContainModifier::scaleFrame() when Imagick::scaleImage() returns false while fitting a frame inside the target box during $image->contain($w, $h). The contain pipeline scales each frame to the crop size before extending the canvas, so a false-returning scale aborts the operation. No previous exception is attached on this path.

Source

Thrown at src/Drivers/Imagick/Modifiers/ContainModifier.php:61

            if ($resize->height() > $crop->height()) {
                $this->fillVerticalAreas($frame->native(), $crop, $resize, $background);
            }
        }

        return $image;
    }

    /**
     * Scale a native Imagick frame to the given dimensions
     *
     * @throws ModifierException
     */
    private function scaleFrame(mixed $native, int $width, int $height): void
    {
        try {
            $result = $native->scaleImage($width, $height);
            if ($result === false) {
                throw new ModifierException(
                    'Failed to apply ' . self::class . ', unable to resize image',
                );
            }
        } catch (ImagickException $e) {
            throw new ModifierException(
                'Failed to apply ' . self::class . ', unable to resize image',
                previous: $e,
            );
        }
    }

    /**
     * Set the background and image background color of a frame to transparent.
     *
     * @throws ModifierException
     */
    private function setFrameBackgroundTransparent(Imagick $native): void
    {

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Raise ImageMagick resource limits (policy.xml or Imagick::setResourceLimit for AREA/MEMORY) sized to your largest expected image.
  2. Pre-downscale oversized sources before contain if full resolution is not required.
  3. Confirm reproducibility via CLI (convert big.png -scale 800x600 out.png) to separate environment issues from code.
  4. Re-read the image fresh from source if the wand may be stale from a prior failed modifier.

Example fix

// before
$image->contain(1200, 900); // scaleImage returns false on resource-capped host

// after
\Imagick::setResourceLimit(\Imagick::RESOURCETYPE_AREA, 512 * 1024 * 1024);
\Imagick::setResourceLimit(\Imagick::RESOURCETYPE_MEMORY, 512 * 1024 * 1024);
$image->contain(1200, 900);
Defensive patterns

Strategy: fallback

Validate before calling

// guard against resource-capped environments before large contains
$needed = $image->width() * $image->height();
$areaCap = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_AREA);
if ($areaCap > 0 && $needed > $areaCap) {
    \Imagick::setResourceLimit(\Imagick::RESOURCETYPE_AREA, $needed * 4);
}

Try / catch

use Intervention\Image\Exceptions\ModifierException;
try {
    $image->contain(1200, 900);
} catch (ModifierException $e) {
    // fallback: pre-downscale then contain again
    $image = $manager->read($source)->scaleDown(2000, 2000)->contain(1200, 900);
}

Prevention

When it happens

Trigger: Calling $image->contain() where scaleImage cannot execute: target dimensions exceeding ImageMagick limits for the pixel cache, degraded wand state, or unsupported image mode. Since contain computes dimensions itself, bad user dimensions rarely reach here directly — it is an environment/wand failure.

Common situations: Contain on very large source images (panoramas, print scans) against tight policy.xml area/memory caps; processing after earlier operations destabilized the wand; shared hosts with low ImageMagick limits.

Related errors


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