About the Image to Base64
Drop, paste or choose a file to convert an image to Base64, and copy it as a data URI, plain Base64, an HTML <img> tag, a CSS background or Markdown. PNG, JPG, GIF, WebP, AVIF, SVG, ICO and BMP all work, and the image never leaves your browser.
Base64 turns the bytes of a file into plain text, so an image can sit inside HTML, CSS, JSON or an email body instead of being a separate file. That's handy for small icons, single-file reports and API payloads. The converter reads the real type from the file's first bytes, not from its name, so a JPEG saved as .png still gets the right image/jpeg data URI. It also shows the dimensions and how much bigger the Base64 is than the file.
- Drop an image onto the box, click Choose Image, or press Ctrl+V (Cmd+V on a Mac) to paste a screenshot from the clipboard. Files up to 10 MB work. Load Example uses toolo's own icon.
- Check the preview and the details under it: file name, type, dimensions and size. A note appears if the file's name doesn't match its real type.
- Pick an output: Data URI, Base64, HTML, CSS or Markdown. The HTML tag includes the image's width and height, and the alt text starts as the file name.
- Click Copy, or Download .txt for a long result. Very long output is cut short in the box to keep the page fast, but Copy and Download always give the full text.
- Choose Another loads a different image, and Clear starts over. You can also drop a new file onto the result.
Inputpixel.png, 1 × 1 px, 91 bytesOutputdata:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAADUlEQVQI12NgYGBgAAAABQABXvMqOgAAAABJRU5ErkJggg==91 bytes become 124 Base64 characters, plus 22 for the data:image/png;base64, prefix. The two = signs at the end are padding: the byte count isn't a multiple of 3.
Inputpixel.png, output HTML, then CSS, then MarkdownOutput<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAADUlEQVQI12NgYGBgAAAABQABXvMqOgAAAABJRU5ErkJggg==" alt="pixel" width="1" height="1">
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACXBIWXMAAAPoAAAD6AG1e1JrAAAADUlEQVQI12NgYGBgAAAABQABXvMqOgAAAABJRU5ErkJggg==");
Setting width and height on the <img> lets the browser reserve the space before the image is decoded, so the page doesn't jump.
Inputphoto.png (the bytes are JPEG)OutputType: JPEG (image/jpeg)
This file is named .png, but it contains JPEG data. The data URI uses image/jpeg, the real type.
data:image/jpeg;base64,/9j/4AA...The extension is only a name. The first bytes, FF D8 FF for JPEG, say what the file is, and a data URI should carry that type.
When a Base64 image makes sense
Base64 uses 4 characters for every 3 bytes, so the text is about 33% bigger than the file. With gzip or Brotli on, as most servers have for HTML and CSS, nearly all of that comes back: a 336,392-byte PNG became 448,524 Base64 characters, and 339,052 bytes after gzip. The real costs are elsewhere:
- Good fits: small icons and logos in CSS, a single HTML file you want to send or archive, images inside JSON for APIs that only take text, and test fixtures.
- No caching of its own: the image downloads again with every page or stylesheet that contains it, while a separate file is fetched once and reused across pages.
- Large images in CSS hold up rendering, because the browser must download the whole stylesheet before it draws anything. Keep inline images to a few kilobytes.
- Email: many clients, including Gmail and Outlook for Windows, don't show data URI images. Attach the image with a Content-ID and use
cid:in thesrc, or link to a hosted file. - SVG: a URL-encoded data URI is usually shorter than Base64. The SVG Viewer makes one with Copy Data URI.
Tell the image type from the first characters
Every format starts with fixed bytes, so its Base64 starts with fixed characters. That helps when you find a long string in a log or a database and want to know what it is:
iVBORw0KGgo: PNG/9j/: JPEGR0lGOD: GIFUklGR: WebP (a RIFF file)PHN2ZyorPD94bWwg: SVG, starting with<svgor<?xmlJVBERi0: PDF, not an image
The data URI format
A data URI, defined in RFC 2397, looks like data:[media type][;base64],[data]. For images the media type is the MIME type, such as image/png or image/svg+xml, and ;base64 says the data part is Base64 rather than URL-encoded text. A plain Base64 string has no type, which is why APIs that accept one usually ask for the type in a separate field.
To turn a string back into a picture, paste it into Base64 to Image, which shows the image, checks its real type and saves it as PNG, JPG, WebP or the original format.
Convert an image to Base64 in code
The same conversion in a few languages. Each builds a data URI; drop the data:...;base64, part when you need plain Base64.
// From an <input type="file"> or a drop event.
function fileToDataUri(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result); // "data:image/png;base64,..."
reader.onerror = () => reject(reader.error);
reader.readAsDataURL(file);
});
}
// The type comes from file.type. An empty type gives data:application/octet-stream.import { readFileSync } from "node:fs";
const base64 = readFileSync("logo.png").toString("base64");
const dataUri = `data:image/png;base64,${base64}`;import base64, mimetypes
path = "logo.png"
mime = mimetypes.guess_type(path)[0]
with open(path, "rb") as f:
data = base64.b64encode(f.read()).decode("ascii")
print(f"data:{mime};base64,{data}")# macOS: one line by default
echo "data:image/png;base64,$(base64 -i logo.png)"
# Linux: -w 0 stops the line breaks every 76 characters
echo "data:image/png;base64,$(base64 -w 0 logo.png)"<?php
$path = "logo.png";
$dataUri = "data:" . mime_content_type($path) . ";base64," . base64_encode(file_get_contents($path));How do I convert an image to Base64?
Drop the image onto the box, choose it, or paste it with Ctrl+V. Pick Data URI or Base64 and click Copy. The conversion runs in your browser.
How much bigger is a Base64 image?
About 33%, because Base64 writes 3 bytes as 4 characters. The details under the preview show the exact sizes, and gzip or Brotli on your server wins back almost all of it.
How do I use a Base64 image in HTML?
Put the data URI in the src attribute: <img src="data:image/png;base64,..." alt="...">. The HTML output gives you the whole tag, with width and height filled in.
How do I use a Base64 image in CSS?
Use it as a URL: background-image: url("data:image/png;base64,...");. Keep it to small icons, because the image downloads again with every stylesheet that contains it.
Can I use Base64 images in emails?
Not reliably. Gmail and Outlook for Windows don't show data URI images. Attach the image with a Content-ID and reference it as cid:..., or link to a hosted image.
How do I convert Base64 back to an image?
Paste the Base64 or data URI into Base64 to Image. It shows the picture, tells you the real type and saves it as PNG, JPG, WebP or the original format.
Is my image uploaded to a server?
No. The file is read and encoded in your browser tab, and nothing is sent anywhere. That matters for screenshots with personal data, ID photos and unreleased designs.