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

JpegEncoder (base class for the driver-specific JPEG encoders) validates $quality in its constructor and throws InvalidArgumentException for any integer below 0 or above 100. Quality reaches it directly via new JpegEncoder(quality: N) or through forwarded options: $image->save('out.jpg', quality: N) resolves the encoder from the extension and constructs it with your options. Note that quality is the first positional parameter, so new JpegEncoder(120) triggers it too.

Source

Thrown at src/Encoders/JpegEncoder.php:24

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

class JpegEncoder 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 $progressive = false,
        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 quality to 0-100 before passing: $q = max(0, min(100, $q))
  2. Validate the parameter at the API/request boundary
  3. Use the named argument (quality:) to avoid positional mix-ups with $progressive

Example fix

// before
$image->save('out.jpg', quality: $request->input('quality'));

// after
$image->save('out.jpg', quality: max(0, min(100, (int) $request->input('quality', 75))));
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: new JpegEncoder(101), $image->save('thumb.jpg', quality: -5), or $image->encodeUsingPath('out.jpg', quality: $userInput) with out-of-range input. Also new JpegEncoder(120, true) when quality is passed positionally.

Common situations: User-facing compression sliders or API parameters forwarded without bounds checking; config drift after deploying a higher default; quality computed from image size or business logic that occasionally exceeds the limit.

Related errors


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