Drag & Drop Image
or Click to Upload
Upload an image and get a Base64 string or a ready-made data URI. Use it for small icons that would otherwise cost an extra request. Avoid it for photographs: Base64 adds about a third to the file size, and an inlined image is cached only as part of the file that contains it.
Drag & Drop Image
or Click to Upload
Base64 encoding writes the image's binary data as a string of ASCII characters. That string can sit inside an HTML or CSS file as a data URI, so the browser gets the image without a separate HTTP request and the document carries its own images.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" /> const fs = require("node:fs");
const path = require("node:path");
const file = "logo.png";
const mime = { ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg",
".gif": "image/gif", ".webp": "image/webp", ".svg": "image/svg+xml" }[path.extname(file)];
const base64 = fs.readFileSync(file).toString("base64");
const dataUri = `data:${mime};base64,${base64}`;
console.log(dataUri.slice(0, 60) + "...");
// In the browser, use FileReader on a File from an <input type="file">:
// const reader = new FileReader();
// reader.onload = () => console.log(reader.result); // full data URI
// reader.readAsDataURL(file); <?php
$file = 'logo.png';
$mime = mime_content_type($file); // image/png
$base64 = base64_encode(file_get_contents($file));
$dataUri = "data:$mime;base64,$base64";
echo substr($dataUri, 0, 60), "...", PHP_EOL;
// Use it directly in markup:
// echo '<img src="' . $dataUri . '" alt="Logo">'; mime_content_type() needs the fileinfo extension, which is enabled by default in most PHP builds.
import base64
import mimetypes
file = "logo.png"
mime = mimetypes.guess_type(file)[0] or "application/octet-stream"
with open(file, "rb") as f:
b64 = base64.b64encode(f.read()).decode("ascii")
data_uri = f"data:{mime};base64,{b64}"
print(data_uri[:60] + "...") mimetypes guesses from the extension only. Use python-magic if you need to sniff the actual bytes.
The format is data:<mime type>;base64,<data>, defined in RFC 2397. Browsers treat it like any other URL: img src, CSS background-image, favicons, even <link> tags. Be sure to get the MIME type right. A PNG labeled image/jpeg renders in some browsers and not others, which makes the bug hard to track down.
Inlining saves an HTTP request. Under HTTP/1.1 that was a clear win, because each request was expensive. With HTTP/2 and HTTP/3 the saving is small, and the cost remains: inlined images cannot be cached on their own or lazy-loaded. We recommend inlining only images under about 5 KB, or an image that blocks first paint, such as a logo above the fold.
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.
PNG, JPEG, GIF, WebP, SVG, BMP and ICO. The tool reads the file and produces a data URI with the correct MIME type.
Paste the data URI into the src attribute of an img tag, or into a CSS url() for background-image. The browser reads the image from the string, so it makes no separate file request.
Inline small assets such as icons, roughly under 5 KB, where saving an HTTP request outweighs the 33% size increase. Keep large images as files so the browser can cache them separately.
No. The file is read with the FileReader API in this tab, so it never leaves your browser.
Yes. The output includes the full data URI; remove the part up to and including the comma to get the raw Base64.
Base64 to Image turns the string back into a file. For text rather than images, use the Base64 encoder.