{"record":{"id":"61011599c7bafaca","repo":"Intervention/image","slug":"unable-to-base64-decode-image-from-string-610115","errorCode":null,"errorMessage":"Unable to Base64-decode image from string","messagePattern":"Unable to Base64-decode image from string","errorType":"exception","errorClass":"Intervention\\Image\\Exceptions\\ImageDecoderException","httpStatus":null,"severity":"error","filePath":"src/Drivers/Imagick/Decoders/Base64ImageDecoder.php","lineNumber":36,"sourceCode":"     *\n     * @see DecoderInterface::supports()\n     */\n    public function supports(mixed $input): bool\n    {\n        return $this->couldBeBase64Data($input);\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see DecoderInterface::decode()\n     */\n    public function decode(mixed $input): ImageInterface\n    {\n        try {\n            $data = $this->decodeBase64Data($input);\n        } catch (DecoderException) {\n            throw new ImageDecoderException('Unable to Base64-decode image from string');\n        }\n\n        try {\n            return parent::decode($data);\n        } catch (DecoderException) {\n            throw new ImageDecoderException('Base64-encoded data contains unsupported image type');\n        }\n    }\n}\n","sourceCodeStart":18,"sourceCodeEnd":46,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Drivers/Imagick/Decoders/Base64ImageDecoder.php#L18-L46","documentation":"Thrown by the Imagick Base64ImageDecoder when its helper decodeBase64Data() rejects the input as not valid Base64 — the string contains characters or padding that strict base64 decoding refuses. It is an ImageDecoderException raised from ImageManager::read() when auto-detection routed a string here.","triggerScenarios":"Passing $manager->read($string) where $string is not pure base64: a full data URI still carrying the 'data:image/png;base64,' prefix; URL-safe base64 (- and _); missing padding; embedded whitespace/newlines from being transmitted in JSON or a textarea; base64 of nothing.","commonSituations":"Frontend sends a data URI (from canvas.toDataURL or a file-reader) and the backend feeds it to read() whole; base64 stored after RFC 4648 URL-safe encoding; strings truncated by URL length limits or copy-paste; payloads re-encoded through form submissions that escape '+' into spaces.","solutions":["Strip the data URI scheme prefix before reading: remove everything through the first comma","Normalize URL-safe alphabet and whitespace: strtr($s, '-_', '+/') and preg_replace('/\\s+/', '', $s)","Validate before decoding with base64_decode($s, true) and a re-encode comparison","If the input is a data URI, pass it as-is — the manager's DataUriImageDecoder handles the scheme; only raw base64 should reach the base64 path"],"exampleFix":"// before: full data URI handed to the base64 path\n$image = $manager->read($dataUriString);\n\n// after: strip scheme, keep raw base64\n$base64 = substr($dataUriString, strpos($dataUriString, ',') + 1);\n$image = $manager->read($base64);\n// or simply: $manager->read($dataUriString) works when passed unchanged,\n// because supports() routes data URIs to DataUriImageDecoder","handlingStrategy":"validation","validationCode":"// keep only pure base64, then verify round-trip\n$s = preg_replace('/\\s+/', '', $input);\n$s = strtr($s, '-_', '+/');\n$decoded = base64_decode($s, true);\nif ($decoded === false || base64_encode($decoded) !== rtrim($s, '=')) {\n    throw new InvalidArgumentException('input is not valid base64');\n}","typeGuard":"function looksLikeBase64(string $s): bool\n{\n    return preg_match('/^[A-Za-z0-9+\\/+]{4}(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=)?$/', $s) === 1;\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\ImageDecoderException;\n\ntry {\n    $image = $manager->read($base64);\n} catch (ImageDecoderException $e) {\n    // either invalid base64 or unsupported payload; report to the uploader\n    return back()->withErrors(['image' => 'Invalid image data']);\n}","preventionTips":["Strip data URI prefixes before treating a string as raw base64","Normalize URL-safe base64 (- _) back to + / and remove whitespace","Prefer passing data URIs untouched to read() so decoder routing handles them"],"tags":["base64","decoding","input-validation","data-uri"],"backgroundTag":"invalid-base64-image-data","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}