Base64 to Image

Paste a Base64 string or a full data URI and see the picture it encodes. Use it when you find image data inside an API response or a stylesheet and want to know what it is before writing code around it. PNG, JPEG, GIF, WEBP and SVG all render.

Base64 Input
Image Preview
Image will appear here

1 Base64 to Image supported formats

Common Images

PNG, JPEG, GIF, WEBP

Vector Graphics

SVG (Scalable Vector Graphics)

2 Example Input

1
data:image/png;base64,iVBORw0...

3 Where the string usually came from

A database column

Check the row before you trust it. Small images are often stored as Base64 text in a database column. Pasting the value here shows whether the row holds a logo or a corrupt blob.

A stylesheet or an email

Data URIs sit inside CSS and HTML. Copy the data: URI out of the url() value and paste it here to see one icon without loading the whole page.

How to use the Base64 to Image tool

  1. Paste a Base64 string into the left editor. The full data URI or just the raw Base64 both work; the tool detects the format from the first bytes.
  2. The image renders in the preview panel on the right. If nothing appears, the string is not a valid image.
  3. Click Download Image to save the decoded file with the correct extension.
  4. Click Upload File to load a .txt file that contains the Base64 string, or Dummy Data to see a sample.
  5. Clear wipes the input and the preview.

How to convert Base64 back to an image file in JavaScript, PHP and Python

JavaScript / Node.js

const fs = require("node:fs");

const input = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";

// Split off the "data:image/png;base64," prefix if present
const match = input.match(/^data:(image\/[a-z+.-]+);base64,(.+)$/i);
const mime = match ? match[1] : "image/png";
const base64 = match ? match[2] : input;

const ext = mime.split("/")[1].replace("+xml", "");
fs.writeFileSync(`decoded.${ext}`, Buffer.from(base64, "base64"));
console.log(`Wrote decoded.${ext}`);

In the browser you do not need to decode at all: set the data URI as the src of an <img> or the href of an <a download>.

PHP

<?php
$input = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';

// Drop the data URI prefix if there is one
if (preg_match('#^data:image/([a-z+.-]+);base64,#i', $input, $m)) {
    $ext = str_replace('+xml', '', $m[1]);
    $input = substr($input, strlen($m[0]));
} else {
    $ext = 'png';
}

$bytes = base64_decode($input, true);
if ($bytes === false) {
    die('Invalid Base64');
}
file_put_contents("decoded.$ext", $bytes);
echo "Wrote decoded.$ext (" . strlen($bytes) . " bytes)", PHP_EOL;

When the Base64 comes from a form or an API, validate the decoded bytes with getimagesizefromstring() before saving, so users cannot upload arbitrary files.

Python

import base64
import re

data = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="

m = re.match(r"^data:image/([a-z+.-]+);base64,(.+)$", data, re.I)
ext = m.group(1).replace("+xml", "") if m else "png"
payload = m.group(2) if m else data

with open(f"decoded.{ext}", "wb") as f:
    f.write(base64.b64decode(payload, validate=True))
print(f"Wrote decoded.{ext}")

Pillow can open the bytes directly with Image.open(io.BytesIO(base64.b64decode(payload))) if you need to resize or convert.

Common errors and how to fix them

The preview is blank and no error is shown
The Base64 decodes but the bytes are not an image the browser understands, for example a PDF, a BMP or a truncated file. Check the first characters: iVBOR is PNG, /9j/ is JPEG, R0lGOD is GIF, UklGR is WebP.
Downloaded file opens as corrupted
The string was cut off when copied, often from a log or a database column with a length limit. A valid PNG ends with the IEND chunk (usually ...SUVORK5CYII=), so compare the tail.
The image is decoded with the wrong extension
The prefix says image/jpeg but the bytes are a PNG. Most viewers open it anyway, but rename it or fix the mime type at the source.
Base64 from a JSON API contains \/ sequences
PHP's json_encode escapes forward slashes by default. The decoded JSON value is correct; if you copied the raw JSON text, unescape it or enable JSON_UNESCAPED_SLASHES on the server.

Good to know

You can tell the format without decoding anything. Every image type starts with a fixed byte signature, and because Base64 maps 3 bytes to 4 characters, that signature always encodes the same way: PNG's 89 50 4E 47 becomes iVBORw0, JPEG's FF D8 FF becomes /9j/.

Store images as files, not as Base64 in a database column. A Base64 image takes a third more space, a CDN cannot serve it, and the browser cannot cache it as a separate resource. Decode the images to files in object storage and keep only the URL in the row.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

Do I need to include the data:image prefix?

No. Paste either a full data URI or the raw Base64 string. The tool detects the image type from the data.

Can I download the decoded image?

Yes. Once the preview appears, click Download Image to save the file in its original format.

Why does the preview not appear?

The string is incomplete, contains stray characters, or is not an image at all. Check that the whole string was copied; a PNG, for example, starts with iVBORw0KGgo. If it is text, the Base64 decoder will show it.

Does it work with SVG?

Yes. SVG data URIs, whether Base64-encoded or URL-encoded, are rendered in the preview.

Is the image sent to a server?

No. The browser decodes and renders the image in this tab, so nothing is uploaded.

Related

Image to Base64 goes the other direction. If the string turns out not to be an image at all, the Base64 decoder shows what it contains.

More Encoders & Decoders

Browse all encoders & decoders

Copied to clipboard!