{"record":{"id":"cfe530a0cb978108","repo":"Intervention/image","slug":"failed-to-read-image-from-stream-cfe530","errorCode":null,"errorMessage":"Failed to read image from stream","messagePattern":"Failed to read image from stream","errorType":"exception","errorClass":"StreamException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Decoders/StreamImageDecoder.php","lineNumber":52,"sourceCode":"     * @throws DriverException\n     * @throws StateException\n     */\n    public function decode(mixed $input): ImageInterface\n    {\n        if (!is_resource($input) || !in_array(get_resource_type($input), ['file', 'stream'])) {\n            throw new InvalidArgumentException('Image source must be a resource of type \"file\" or \"stream\"');\n        }\n\n        $contents = '';\n        $rewind = rewind($input);\n        if ($rewind === false) {\n            throw new StreamException('Failed to rewind position of stream');\n        }\n\n        while (!feof($input)) {\n            $chunk = fread($input, 1024);\n            if ($chunk === false) {\n                throw new StreamException('Failed to read image from stream');\n            }\n\n            $contents .= $chunk;\n        }\n\n        try {\n            return parent::decode($contents);\n        } catch (DecoderException) {\n            throw new ImageDecoderException(\n                'Failed to decode image from stream, could be unsupported image format',\n            );\n        }\n    }\n}\n","sourceCodeStart":34,"sourceCodeEnd":67,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Decoders/StreamImageDecoder.php#L34-L67","documentation":"fread() returned false while the decoder drained the stream in 1024-byte chunks - an actual read error mid-stream (socket reset, TLS failure, wrapper error), distinct from a clean EOF which ends the loop normally. Thrown as StreamException before any image decoding is attempted.","triggerScenarios":"A network stream (fopen('https://...')) where the server drops the connection mid-body; S3/FTP stream wrappers whose credentials expire or time out during transfer; a stream left in an error state by a prior operation.","commonSituations":"Remote image fetching with unreliable sources; default_socket_timeout too low for slow origins; partial uploads read from php://input before the body fully arrived.","solutions":["Fetch the full body with an HTTP client that verifies completeness (Content-Length check) and pass the string","Inspect stream_get_meta_data($input) for 'timed_out' and wrapper data to find the underlying cause","Raise default_socket_timeout or the wrapper's timeout option for slow sources","Retry the fetch - transient network resets are common"],"exampleFix":"// before\n$image = $manager->read(fopen('https://slow.example.com/big.jpg', 'rb'));\n\n// after\n$response = $httpClient->request('GET', 'https://slow.example.com/big.jpg', ['timeout' => 30]);\n$image = $manager->read($response->getContent());","handlingStrategy":"retry","validationCode":"$meta = stream_get_meta_data($stream);\nif (!empty($meta['timed_out'])) {\n    throw new RuntimeException('Stream timed out mid-read');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $image = $manager->read($stream);\n} catch (\\Intervention\\Image\\Exceptions\\StreamException $e) {\n    // transient network failure: close, re-open, retry once with backoff\n}","preventionTips":["Fetch remote images via an HTTP client with timeouts and retries; pass the body string","Set default_socket_timeout (or wrapper context timeout) generously for large images"],"tags":["stream","fread","network","read-error"],"backgroundTag":"stream-read-failed","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}