Image to Base64 Converter

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.

Image Preview

Drag & Drop Image
or Click to Upload

Base64 Output

1 How Base64 image encoding works

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.

2 Example Usage

Embedding in HTML

1
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" />

3 Pros & Cons

Pros

  • Fewer requests: small images arrive with the HTML or CSS that contains them.
  • No broken links: the image travels inside the file, so a moved asset cannot break it.
  • Text storage: the string fits in any text column or JSON field.

Cons

  • Size: the string is about 33% larger than the original file.
  • Caching: editing the stylesheet also re-downloads every image inlined in it.

How to use the Image to Base64 tool

  1. Click Upload Image or drag an image onto the upload area. PNG, JPEG, GIF, WebP and SVG all work.
  2. A preview appears with the file name and size, and the editor fills with the full data URI (data:image/png;base64,...).
  3. Click Copy Base64 to copy the string and paste it into an <img src> or a CSS url().
  4. The file is read with the browser's FileReader API in this tab, so nothing leaves your machine. Click Clear to drop the image and start over.

How to convert an image to a Base64 data URI in JavaScript, PHP and Python

JavaScript / Node.js

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

<?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.

Python

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.

Common errors and how to fix them

The image does not show and the URL bar shows data:image/png;base64,data:image...
The prefix was added twice. The tool already outputs a full data URI, so paste it as-is into src, without typing the prefix again.
Selector or declaration is too long in a CSS linter, or the stylesheet editor freezes
Large images produce data URIs of hundreds of kilobytes on one line. Inline only small icons; keep photos and large graphics as separate files.
SVG shows in the tool but is blank in the page
The SVG probably lacks width and height or a viewBox, so it renders at 0 size. Add a viewBox attribute to the root element before converting.
Page total size doubled after inlining images
Base64 adds 33 percent, and inlined images are not cached separately from the HTML or CSS. Every page load re-downloads them, so measure before inlining.

Good to know

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.

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.

Guides and tutorials

Frequently Asked Questions

Which image formats are supported?

PNG, JPEG, GIF, WebP, SVG, BMP and ICO. The tool reads the file and produces a data URI with the correct MIME type.

How do I use the result in HTML or CSS?

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.

When should I inline an image?

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.

Is the image uploaded?

No. The file is read with the FileReader API in this tab, so it never leaves your browser.

Can I copy just the raw Base64 without the data: prefix?

Yes. The output includes the full data URI; remove the part up to and including the comma to get the raw Base64.

Related

Base64 to Image turns the string back into a file. For text rather than images, use the Base64 encoder.

More Encoders & Decoders

Browse all encoders & decoders

Copied to clipboard!