Intervention/image · error · InvalidArgumentException
Invalid sharpening level. Must be int<0, max>
Error message
Invalid sharpening level. Must be int<0, max>
What it means
SharpenModifier backs $image->sharpen($level) and accepts any int greater than or equal to 0. The level controls sharpening intensity; 0 is a valid no-op value, but negative values have no defined meaning, so the constructor throws InvalidArgumentException with the message 'Invalid sharpening level. Must be int<0, max>' (the int<0, max> notation is the PHPStan range for non-negative integers).
Source
Thrown at src/Modifiers/SharpenModifier.php:18
<?php
declare(strict_types=1);
namespace Intervention\Image\Modifiers;
use Intervention\Image\Drivers\SpecializableModifier;
use Intervention\Image\Exceptions\InvalidArgumentException;
class SharpenModifier extends SpecializableModifier
{
/**
* @throws InvalidArgumentException
*/
public function __construct(public int $level)
{
if ($this->level < 0) {
throw new InvalidArgumentException('Invalid sharpening level. Must be int<0, max>');
}
}
}
View on GitHub (pinned to 5598b9e397)
Solutions
- Clamp the level to 0 before calling: $image->sharpen(max(0, $level))
- Cast and validate external input: $level = (int) $input; if ($level < 0) reject or normalize
- If a negative value means 'blur' in your domain, translate it to the appropriate blur() call instead of passing it to sharpen()
Example fix
// before
$image->sharpen($request->input('sharpen'));
// after
$level = max(0, (int) $request->input('sharpen'));
$image->sharpen($level); Defensive patterns
Strategy: validation
Validate before calling
// Clamp before sharpening
$level = (int) $input['sharpen'];
if ($level < 0) {
$level = 0; // or reject the request
}
$image->sharpen($level); Type guard
function isValidSharpenLevel(mixed $level): bool
{
return is_int($level) && $level >= 0;
} Try / catch
use Intervention\Image\Exceptions\InvalidArgumentException;
try {
$image->sharpen($level);
} catch (InvalidArgumentException $e) {
// message: 'Invalid sharpening level. Must be int<0, max>'
$image->sharpen(0);
} Prevention
- Treat sharpen level as an unsigned int in your input validation (min: 0)
- Clamp external values with max(0, (int) $value) before passing them to sharpen()
- Keep sharpen presets in config and validate them once at load time
When it happens
Trigger: Calling $image->sharpen(-1) or new SharpenModifier(-10). Commonly the level is computed from user input, a signed calculation, or a config value that was mistyped with a minus sign.
Common situations: UI sliders or API parameters that allow the user to submit a negative sharpen amount; arithmetic on a delta (e.g. sharpen($current - $amount)) that underflows below 0; configuration files copied from another tool where the sign convention differs.
Related errors
- At least one argument must be provided: width, height, or bo
- Length must be greater than or equal to 1
- Invalid trim tolerance. Must be int<0, max>
- The value of the X-axis for {class} must be greater or equal
- The value of the Y-axis for {class} must be greater or equal
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/e2f15cb931606823.
Report an issue: GitHub.