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

InvalidArgumentException from the AvifEncoder constructor: quality is validated to be an integer in the inclusive range 0-100, and anything outside is rejected immediately at construction time, before any encoding happens. The same 0-100 percent contract applies to the other quality-taking encoders in this library.

Source

Thrown at src/Encoders/AvifEncoder.php:23

namespace Intervention\Image\Encoders;

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

class AvifEncoder 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 before constructing: $quality = max(0, min(100, (int) $quality))
  2. Validate the range at the request boundary and reject out-of-range input early
  3. Check for scale confusion - this library uses 0-100 percent, not 0-1 or crf-style values
  4. Use the named argument (quality: 85) to avoid positional-argument mix-ups with strip

Example fix

// before
$encoded = $image->encode(new AvifEncoder(150));

// after
$quality = max(0, min(100, (int) $config['quality']));
$encoded = $image->encode(new AvifEncoder(quality: $quality));
Defensive patterns

Strategy: validation

Validate before calling

$quality = max(0, min(100, (int) $input['quality']));
$encoded = $image->encode(new AvifEncoder(quality: $quality));

Type guard

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

Try / catch

use Intervention\Image\Exceptions\InvalidArgumentException as ImageInvalidArgumentException;

try {
    $encoder = new AvifEncoder(quality: $quality);
} catch (ImageInvalidArgumentException $e) {
    $encoder = new AvifEncoder(); // fall back to default quality
}

Prevention

When it happens

Trigger: new AvifEncoder(quality: 150) or quality: -1; in practice $image->encode(new AvifEncoder(quality: (int) $request->input('quality'))) with unchecked user input, or config values ported from tools that use a different scale.

Common situations: User-supplied quality from forms/APIs passed through unchecked; environment variables or configs written for a 0-1 or 0-10 scale (ffmpeg crf, some encoders); arithmetic that adds a margin to an already-maximal 100.

Related errors


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