Intervention/image · error · StreamException
Failed to read image from stream
Error message
Failed to read image from stream
What it means
During the read loop, fread() on the stream returned false, which is a hard read error rather than a clean EOF. The stream claimed data was available (feof() false) but reading failed — typical for sockets that error mid-transfer or handles opened in a non-readable mode.
Source
Thrown at src/Drivers/Gd/Decoders/StreamImageDecoder.php:56
* @throws NotSupportedException
*/
public function decode(mixed $input): ImageInterface
{
if (!is_resource($input) || !in_array(get_resource_type($input), ['file', 'stream'])) {
throw new InvalidArgumentException("Image source must be a resource of type 'file' or 'stream'");
}
$contents = '';
$result = rewind($input);
if ($result === false) {
throw new StreamException('Failed to rewind position of stream');
}
while (!feof($input)) {
$chunk = fread($input, 1024);
if ($chunk === false) {
throw new StreamException('Failed to read image from stream');
}
$contents .= $chunk;
}
try {
return parent::decode($contents);
} catch (DecoderException) {
throw new ImageDecoderException(
'Failed to decode image from stream, could be unsupported image format',
);
}
}
}
View on GitHub (pinned to 5598b9e397)
Solutions
- Open files in a read mode: fopen($path, 'rb')
- Fetch remote content with a retrying HTTP client, then decode the string
- Verify stream_get_meta_data($fp)['mode'] contains 'r' before decoding
- Catch StreamException and re-fetch or retry the source once before giving up
Example fix
// before $fp = fopen($path, 'wb'); // write-only handle $image = $manager->decodeStream($fp); // fread fails // after $fp = fopen($path, 'rb'); $image = $manager->decodeStream($fp);
Defensive patterns
Strategy: validation
Validate before calling
$meta = stream_get_meta_data($fp);
if (strpos($meta['mode'], 'r') === false && strpos($meta['mode'], '+') === false) {
throw new RuntimeException('Stream is not readable: mode ' . $meta['mode']);
} Try / catch
try {
$image = $manager->decodeStream($fp);
} catch (StreamException $e) {
// re-open the source in 'rb' mode or re-fetch and retry once
} Prevention
- Open files with fopen($path, 'rb') for reading
- Use retrying HTTP clients for network sources
- Do not reuse write-mode handles for reading
When it happens
Trigger: A socket/HTTP stream whose connection drops mid-read; a handle opened with mode 'w'/'wb' (write-only) so fread() fails; a stream that became invalid between the feof() check and the read.
Common situations: Unstable networks while streaming remote images, write-only log handles passed by mistake, mode flags typo'd like fopen($p, 'wb') later reused for reading.
Related errors
- Image source must be a resource of type 'file' or 'stream'
- Failed to rewind position of stream
- Failed to decode image from stream, could be unsupported ima
- Failed to read image from stream
- Class '{objectShortname}' is not supported by {id} driver
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/1009dec7b75c68e0.
Report an issue: GitHub.