Encoding // blog
All blog postsHow to Convert Images to Base64 Data URIs (and Decode Base64 Back to Images)
Image to Base64 Converter / Base64 to Image ConverterEvery developer eventually finds a Base64 string in the wild. It is tucked inside a JSON payload, sitting in an email template, or staring back from a stylesheet where someone embedded a tiny icon so the browser would not make another HTTP request. Sometimes you want a data URI. Sometimes you just want to see what that giant text blob actually is without saving a sketchy file to disk.
The Image to Base64 Converter / Base64 to Image Converter does both directions in this tab. We never see the image.
Encode images to Base64 and decode them back
Drop in a PNG, JPEG, GIF, or WebP to get a raw Base64 string, a full data:image/...;base64,... URI, CSS background-image, or an img tag. Paste a Base64 string or data URI to preview and download PNG, JPG, or WebP.
Working with vectors? Use the SVG to Base64 & CSS Data URI Encoder / Decoder. Plain text or tokens belong on the Base64 Encoder / Decoder. Need a PNG first? Convert WebP in the WebP to PNG Converter / PNG to WebP Converter. Those tools also run on your computer.
What a Base64 data URI is
Base64 takes binary image data and writes it with 64 ASCII characters (A-Z, a-z, 0-9, +, /, and = padding). Wrap it in a data URI and the string can sit in an img src or a CSS url(...):
Data URI
data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...When data URIs actually help
| Use | Why |
|---|---|
| Tiny icons and badges | Skip an extra request when the string stays small. |
| Single-file embeds | HTML emails, JSON, or config that must carry the image. |
| Quick debugging | Check that an API Base64 payload is a real image. |
The catch: about 33% larger
Base64 is encoding, not compression. Binary grows by about a third, and a fat string inside HTML or CSS cannot be cached as its own asset. Keep data URIs lean. Use a regular image file when the payload gets chunky.
How to convert images to Base64 by hand
The converter is the five-second path. FileReader, Blob, and the terminal still help in scripts, API tests, and DevTools.
1. File to data URI with FileReader
In the browser, readAsDataURL() turns a File or Blob into data:<mime>;base64,<data>:
JavaScript
function fileToDataUrl(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
if (typeof reader.result === "string") {
resolve(reader.result);
} else {
reject(new Error("Unexpected FileReader result"));
}
};
reader.onerror = () => reject(reader.error || new Error("Failed to read file"));
reader.readAsDataURL(file);
});
}
// Raw payload after the comma:
// const rawBase64 = dataUrl.split(",")[1];Drop that data URI into an img or CSS:
HTML
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==" alt="Tiny sample pixel" />2. Decode Base64 back into an image
A full data URI or raw Base64 with a known MIME type (for example image/png) becomes a Blob you can preview or download:
JavaScript
function base64ToBlob(base64OrDataUrl, fallbackMime = "image/png") {
let mimeType = fallbackMime;
let base64Data = base64OrDataUrl.trim();
if (base64Data.startsWith("data:")) {
const match = base64Data.match(/^data:([^;]+);base64,(.+)$/);
if (!match) {
throw new Error("Invalid data URI format");
}
mimeType = match[1];
base64Data = match[2];
}
const binaryString = atob(base64Data);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return new Blob([bytes], { type: mimeType });
}
const blob = base64ToBlob(someBase64String, "image/png");
const objectUrl = URL.createObjectURL(blob);Call URL.revokeObjectURL(objectUrl) when you are done so the temporary URL does not leak.
3. Encode and decode in the terminal
macOS / Linux
# Encode a PNG to a single line
base64 -i sample.png | tr -d '\n'
# Decode back to a PNG
echo "<PASTE_BASE64_HERE>" | base64 --decode > decoded.pngPowerShell
[Convert]::ToBase64String([IO.File]::ReadAllBytes("sample.png"))
[IO.File]::WriteAllBytes("decoded.png", [Convert]::FromBase64String($raw))4. When the preview looks broken
- Missing prefix. An
imgneeds a fulldata:image/png;base64,...URI, not raw Base64 by itself. - Whitespace. Accidental newlines or spaces trip up strict decoders. Strip them first. The converter already unwraps CSS and HTML paste.
- Wrong MIME type. Decoding a JPEG as
image/pngdoes not turn it into a PNG. Match the actual format, or download PNG/JPG/WebP from the converter after it redraws the bitmap.
Encode a swatch or decode a payload in the Image to Base64 Converter / Base64 to Image Converter. The conversion stays on your computer. We never see the files.
Share