{"record":{"id":"1009dec7b75c68e0","repo":"Intervention/image","slug":"failed-to-read-image-from-stream","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/Gd/Decoders/StreamImageDecoder.php","lineNumber":56,"sourceCode":"     * @throws NotSupportedException\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        $result = rewind($input);\n\n        if ($result === 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":38,"sourceCodeEnd":71,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Gd/Decoders/StreamImageDecoder.php#L38-L71","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\n$fp = fopen($path, 'wb'); // write-only handle\n$image = $manager->decodeStream($fp); // fread fails\n\n// after\n$fp = fopen($path, 'rb');\n$image = $manager->decodeStream($fp);","handlingStrategy":"validation","validationCode":"$meta = stream_get_meta_data($fp);\nif (strpos($meta['mode'], 'r') === false && strpos($meta['mode'], '+') === false) {\n    throw new RuntimeException('Stream is not readable: mode ' . $meta['mode']);\n}","typeGuard":null,"tryCatchPattern":"try {\n    $image = $manager->decodeStream($fp);\n} catch (StreamException $e) {\n    // re-open the source in 'rb' mode or re-fetch and retry once\n}","preventionTips":["Open files with fopen($path, 'rb') for reading","Use retrying HTTP clients for network sources","Do not reuse write-mode handles for reading"],"tags":["stream","fread","io-error","gd"],"backgroundTag":"stream-read-failed","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}