Skip to the content

Image to Base64

Turn an image into a Base64 data URI for HTML or CSS, or decode one back.

Share this tool:TwitterFacebookLinkedInWhatsAppEmail

What a data URI is

A data URI puts a whole file in the place where its address would go. Instead of src="logo.png" you write src="data:image/png;base64,…", and the browser takes the picture from the text itself without fetching anything. The part before the comma names the kind of file. The part after it is the file, written in Base64.

The tool writes out the bytes of your file as they are. The picture is not decoded, resized or compressed again, and it is not uploaded: your browser does the conversion.

Why the text is a third larger

A file consists of bytes, and a byte can have 256 different values. Base64 uses only 64 characters that pass unharmed through anything that handles text: A to Z, a to z, 0 to 9, + and /. One character therefore carries 6 bits where a byte carries 8, and every 3 bytes of the file become 4 characters. When the size isn't a multiple of 3, the end is padded with =.

A 3,000-byte icon becomes 4,000 characters, plus 22 for data:image/png;base64,. That is 33% more to send. Compression on the web server (gzip or Brotli) takes back most of the difference.

When inlining pays off

  • Very small pictures, up to a few kB: an icon, a pattern, a placeholder. Every separate file costs a request, and for a few hundred bytes the request can cost more than the picture.
  • A page that has to work as one file, such as an HTML report you send to someone.

When it doesn't

  • Anything larger than a few kB. Inlined in HTML, a picture is downloaded again with every page view, while a separate file is fetched once and then comes from the browser's cache. Inlined in a stylesheet it is cached along with the stylesheet, but the browser waits for the whole stylesheet before it shows the page, so a large picture in it holds everything up.
  • E-mail. It looks like the obvious use, but Gmail doesn't show pictures embedded this way, and neither does Outlook for Windows up to the 2016 version. Apple Mail and Thunderbird do. Can I email keeps the list. For a newsletter, link to pictures on a server.
  • SVG. It is text already. URL-encoding it gives a shorter result than Base64, and one you can still read. SVG to CSS does that.

From Base64 back to a picture

Paste a data URI, a piece of HTML or CSS with one in it, or bare Base64. Line breaks in the text don't matter.

What kind of picture it is, is read from the first bytes of the data and not from the label in front. A JPEG that was labeled image/png is reported as the JPEG it is. When a PNG, JPEG or WebP ends before the picture does, which is what happens when a long text is cut off while copying, the tool says so. An SVG is shown as a picture and never run as code.

See all 8