{"record":{"id":"8bbab16e91243301","repo":"Intervention/image","slug":"failed-to-decode-unsupported-image-format-from-bin-8bbab1","errorCode":null,"errorMessage":"Failed to decode unsupported image format from binary data","messagePattern":"Failed to decode unsupported image format from binary data","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\ImageDecoderException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Decoders/BinaryImageDecoder.php","lineNumber":60,"sourceCode":"    public function decode(mixed $input): ImageInterface\n    {\n        if (!is_string($input) && !$input instanceof Stringable) {\n            throw new InvalidArgumentException(\n                'Image source must be binary data of type string or instance of ' . Stringable::class,\n            );\n        }\n\n        $input = (string) $input;\n\n        if ($input === '') {\n            throw new InvalidArgumentException('Unable to decode binary data from empty string');\n        }\n\n        try {\n            $imagick = new Imagick();\n            $imagick->readImageBlob($input);\n        } catch (ImagickException) {\n            throw new ImageDecoderException('Failed to decode unsupported image format from binary data');\n        }\n\n        // decode image\n        $image = parent::decode($imagick);\n\n        // get media type enum from string media type\n        $format = Format::tryCreate($image->origin()->mediaType());\n\n        // extract exif data for appropriate formats\n        if (in_array($format, [Format::JPEG, Format::TIFF])) {\n            $image->setExif($this->extractExifData($input));\n        }\n\n        return $image;\n    }\n}\n","sourceCodeStart":42,"sourceCodeEnd":77,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Decoders/BinaryImageDecoder.php#L42-L77","documentation":"Thrown by BinaryImageDecoder::decode() when new Imagick + readImageBlob($input) throws — the byte string is not data ImageMagick can parse as an image. The original ImagickException is swallowed (no previous), so the exact native reason is not visible. This is the workhorse 'not an image' error for binary input on the Imagick driver.","triggerScenarios":"$manager->read($binary) where $binary is corrupt (truncated download, bit-flipped upload), a non-image file renamed to .png, HTML/XML error output from a misconfigured URL, or a real image in a format the installed ImageMagick cannot decode (AVIF/HEIC/WebP without delegates, CMYK variants on old builds).","commonSituations":"Scraping pipelines capturing 404 pages as 'images'; partial uploads with Content-Length mismatches; deployments where the imagick extension's underlying libmagick lacks delegates present on dev machines; SVG payloads when rsvg is missing.","solutions":["Verify the bytes with a signature sniff before calling read() (finfo, getimagesize, or manual magic bytes)","Confirm the source actually transferred fully (check download size vs Content-Length)","Run convert -list format on the server and install delegates for the formats you accept","Catch ImageDecoderException around read() and reject/log the source"],"exampleFix":"// before: trusting remote bytes\n$image = $manager->read($http->get($url)->getBody());\n\n// after: sniff the format first\n$bytes = (string) $http->get($url)->getBody();\n$mime = (new finfo(FILEINFO_MIME_TYPE))->buffer($bytes);\nif (!str_starts_with($mime, 'image/')) {\n    throw new InvalidArgumentException(\"Source at {$url} is {$mime}, not an image\");\n}\n$image = $manager->read($bytes);","handlingStrategy":"try-catch","validationCode":"$mime = (new finfo(FILEINFO_MIME_TYPE))->buffer($bytes);\nif (!str_starts_with((string) $mime, 'image/')) {\n    throw new InvalidArgumentException(\"refusing non-image payload ({$mime})\");\n}\n$image = $manager->read($bytes);","typeGuard":null,"tryCatchPattern":"use Intervention\\Image\\Exceptions\\ImageDecoderException;\n\ntry {\n    $image = $manager->read($bytes);\n} catch (ImageDecoderException $e) {\n    Log::notice('undecodable image rejected', ['bytes' => strlen($bytes)]);\n    return response()->json(['error' => 'image could not be decoded'], 422);\n}","preventionTips":["MIME-sniff every remote/scraped byte string before decoding","Verify transfer completeness (Content-Length vs actual) for downloaded images","Audit production ImageMagick delegates when adding a format to your allowlist"],"tags":["imagick","unsupported-format","corrupt-file","decoding"],"backgroundTag":"corrupt-or-unsupported-image-data","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}