Intervention/image · error · InvalidArgumentException
Invalid data uri scheme
Error message
Invalid data uri scheme
What it means
DataUri::parse() applies a strict regex for RFC 2397 data URIs: a 'data:' prefix, an optional mediaType, optional ;-separated parameters, an optional ';base64' marker, then a comma and the payload. Any string not matching that shape fails the regex and throws this InvalidArgumentException.
Source
Thrown at src/DataUri.php:78
mediaType: $mediaType,
parameters: $parameters,
base64: $base64,
);
}
/**
* {@inheritdoc}
*
* @see DataUriInterface::parse()
*
* @throws InvalidArgumentException
*/
public static function parse(string|Stringable $dataUriScheme): self
{
$result = preg_match(self::PATTERN, (string) $dataUriScheme, $matches);
if ($result === false || $result === 0) {
throw new InvalidArgumentException('Invalid data uri scheme');
}
$isBase64Encoded = $matches['base64'] !== '';
$datauri = new self(
data: $isBase64Encoded ? base64_decode($matches['data'], strict: true) : rawurldecode($matches['data']),
mediaType: $matches['mediaType'],
base64: $isBase64Encoded,
);
if ($matches['parameters'] !== '') {
$parameters = explode(';', $matches['parameters']);
$parameters = array_filter($parameters, fn(string $value): bool => $value !== '');
$parameters = array_map(fn(string $value): array => explode('=', $value), $parameters);
foreach ($parameters as $parameter) {
$datauri->setParameter(...$parameter);
}
}View on GitHub (pinned to 5598b9e397)
Solutions
- Ensure the string is a complete data URI: data:[mediaType][;parameters][;base64],<data>
- If the input is a URL, fetch its contents instead of parsing it as a data URI
- Trim and URL-decode the incoming value before parsing
Example fix
// before
$uri = DataUri::parse($request->input('avatar'));
// after
$value = trim((string) $request->input('avatar'));
$uri = str_starts_with($value, 'data:')
? DataUri::parse($value)
: DataUri::create(file_get_contents($value)); Defensive patterns
Strategy: validation
Validate before calling
function isDataUri(string $value): bool
{
return preg_match('#^data:[\w/+.-]*(;[\w-=]+)*(;base64)?,.+#', $value) === 1;
}
if (!isDataUri($candidate)) {
throw new \RuntimeException('Expected a data URI');
} Try / catch
try {
$uri = DataUri::parse($value);
} catch (\Intervention\Image\Exceptions\InvalidArgumentException $e) {
// not a data URI - fetch instead if it is a URL, or reject the input
} Prevention
- Check for the 'data:' prefix and a comma before parsing
- Trim and URL-decode transport values first
- Do not pass regular URLs to DataUri::parse()
When it happens
Trigger: Passing 'data:image/png;base64' (missing comma and payload), 'image/png;base64,...' (missing 'data:' prefix), a normal URL ('https://example.com/a.png'), or a media type containing characters outside [-+.\w] such as spaces.
Common situations: Data URLs truncated by transport or copy-paste; frontends sending regular URLs where the backend expects a data URI; URL-encoded whitespace sneaking into the media type.
Related errors
- Data Uri contains unsupported image type
- Unable to parse RGB color from input "{input}"
- Invalid cmyk() color syntax "{input}"
- Unable to parse HSL color from input "{input}"
- Invalid hsl() color syntax "{input}"
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/aeb474424fbd20f9.
Report an issue: GitHub.