Intervention/image · error · Intervention\Image\Exceptions\InvalidArgumentException

Color channel {class} value must be in range {min} to {max}

Error message

Color channel {class} value must be in range {min} to {max}

What it means

DrawPolygonModifier requires the Polygon it receives to contain at least 3 points, because a polygon with fewer vertices cannot be rasterized. The check runs in the constructor, so $image->drawPolygon() fails immediately when handed a degenerate Polygon.

Source

Thrown at src/Colors/AbstractColorChannel.php:62

        $normalized = $this->normalized();
        $base = $percent >= 0 ? (1 - $normalized) : $normalized;
        $scaled = min(1.0, max(0.0, $normalized + $base / 100 * $percent));
        $this->value = static::fromNormalized($scaled)->value();

        return $this;
    }

    /**
     * Throw exception if the given value is not applicable for channel
     * otherwise the value is returned unchanged.
     *
     * @throws InvalidArgumentException
     */
    protected function validValueOrFail(int|float $value): mixed
    {
        if ($value < $this->min() || $value > $this->max()) {
            throw new InvalidArgumentException(
                'Color channel ' . $this::class . ' value must be in range ' . $this->min() . ' to ' . $this->max(),
            );
        }

        return $value;
    }

    /**
     * {@inheritdoc}
     *
     * @see ColorChannelInterface::toString()
     */
    public function toString(): string
    {
        return (string) $this->value();
    }

    /**

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Count before drawing: if ($polygon->count() >= 3) { $image->drawPolygon($polygon); }.
  2. Skip or log degenerate datasets instead of drawing them.
  3. If points were removed by validation, treat fewer than 3 remaining points as 'nothing to draw', not as a draw call.

Example fix

// before
$image->drawPolygon($polygon); // count() === 2 throws

// after
if ($polygon->count() >= 3) {
    $image->drawPolygon($polygon);
}
Defensive patterns

Strategy: validation

Validate before calling

if ($polygon->count() < 3) {
    // nothing drawable; skip or log
    return;
}
$image->drawPolygon($polygon);

Type guard

function isDrawablePolygon(Intervention\Image\Geometry\Polygon $polygon): bool
{
    return $polygon->count() >= 3;
}

Prevention

When it happens

Trigger: $image->drawPolygon(new Polygon($points)) where $points has 0-2 entries; building points dynamically from data that can collapse (filtered results, empty datasets, division producing coincident points reduced by dedup).

Common situations: Chart/map overlays where a region's coordinates come from a database query that returned one row; array_filter() removing invalid points below the threshold; empty input arrays from failed upstream parsing.

Related errors


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