Intervention/image · error · InvalidArgumentException

Quality must be in range 0 to 100

Error message

Quality must be in range 0 to 100

What it means

Jpeg2000Encoder validates its $quality constructor option and throws InvalidArgumentException unless it is an integer between 0 and 100 inclusive. The value arrives either directly via new Jpeg2000Encoder(quality: N) or through option forwarding from $image->save('out.jp2', quality: N) / encodeUsingPath(). The range check happens in the shared constructor before driver specialization, so it throws on any driver even though JPEG 2000 output needs Imagick.

Source

Thrown at src/Encoders/Jpeg2000Encoder.php:23

namespace Intervention\Image\Encoders;

use Intervention\Image\Drivers\SpecializableEncoder;
use Intervention\Image\Exceptions\InvalidArgumentException;

class Jpeg2000Encoder extends SpecializableEncoder
{
    /**
     * Create new encoder object.
     *
     * @param null|bool $strip Strip EXIF metadata
     * @throws InvalidArgumentException
     */
    public function __construct(
        public int $quality = self::DEFAULT_QUALITY,
        public ?bool $strip = null,
    ) {
        if ($quality < 0 || $quality > 100) {
            throw new InvalidArgumentException('Quality must be in range 0 to 100');
        }
    }
}

View on GitHub (pinned to 5598b9e397)

Solutions

  1. Clamp the quality: $quality = max(0, min(100, $quality))
  2. Add validation rules for any quality value coming from users or configuration
  3. If a fractional 0-1 scale is used internally, convert it explicitly: (int) round($ratio * 100)

Example fix

// before
$enc = new Jpeg2000Encoder(quality: $ratio * 100 * 100); // double scaling -> 10000

// after
$enc = new Jpeg2000Encoder(quality: (int) round(max(0, min(1, $ratio)) * 100));
Defensive patterns

Strategy: validation

Validate before calling

$quality = max(0, min(100, (int) $quality));
$image->save('scan.jp2', quality: $quality);

Type guard

function isValidQuality(mixed $quality): bool
{
    return is_int($quality) && $quality >= 0 && $quality <= 100;
}

Prevention

When it happens

Trigger: new Jpeg2000Encoder(150), new Jpeg2000Encoder(quality: -10), or $image->encodeUsingPath('scan.jp2', quality: $value) with $value outside 0-100.

Common situations: Quality configured from a settings panel or .env entry that was never range-validated; unit-to-unit scaling bugs that produce values above 100; negative quality from misconfigured defaults.

Related errors


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