Browser-based image tool · base64 data URIs

Image to Base64 Data URI

Turn an image into a base64 data URI with HTML, CSS, Markdown and JSON snippets, or paste a data URI to see the image and save it as a file. Encoding and decoding are done by this tab.

Image

One PNG, JPG, WEBP, GIF or SVG, up to 10.00 MB. The type is checked from the file contents.

Drop an image here or click to choose

.png, .jpg, .jpeg, .webp, .gif, .svg

Data URI

Add an image to encode it.

The data URI and the HTML, CSS, Markdown and JSON snippets appear here.

Image to Base64 Data URI

Operator: Abdulla Al Fahad. How tools are built and checked

A data URI puts a whole file inside a line of text: data:image/png;base64, followed by the file's bytes written in a 64-character alphabet. That lets a small icon travel inside an HTML page, a stylesheet, a JSON payload or a Markdown note with no second request and no separate file to host. It also makes pasting an image into places that only accept text, such as an email template, a config field or a chat message to a teammate, possible.

This page does both directions. Give it a PNG, JPG, WEBP, GIF or SVG and it returns the data URI, the byte count before and after, and four ready snippets: an HTML img tag, a CSS background-image, a Markdown image and a JSON string. Paste a data URI or raw base64 into the other tab and it checks the text, works out what kind of image it really is from the bytes, shows the picture and lets you download it as a file.

Key features

  • Base64 data URI from PNG, JPG, WEBP, GIF or SVG files up to 10 MB, with the type checked from the bytes
  • Extra URL-encoded data URI for SVG, with the length of both forms shown
  • File size, URI length and the percentage increase for your image
  • HTML img, CSS background-image, Markdown and JSON snippets, each with its own copy button
  • Optional maximum width and output format (PNG, JPG or WEBP) for raster images before encoding
  • Decoder for data URIs and raw base64 with specific errors for bad characters, misplaced padding and truncated text
  • Image preview and download of the decoded file with the extension that matches its real type
  • Warning when a data URI passes 100 KB

Size overhead, type detection and decoding

Base64 turns every 3 bytes into 4 characters, so the text is roughly a third longer than the file, and the data: prefix adds a few more. A 70-byte PNG becomes 96 base64 characters; with the 22-character prefix data:image/png;base64, the URI is 118 characters, about 69 percent more than the file. On a larger image the overhead settles near 33 percent. The page shows the exact length and the percentage for your file.

PNG, JPEG, WEBP and GIF are recognised from their opening bytes, not from the file name or extension. A file named logo.png that is really a JPG is encoded with the JPEG type, so the browser reads it correctly. SVG is text and has no binary signature, so a file counts as SVG if, after any BOM, XML declaration, comment or doctype, it begins with an svg element.

SVG gets a second form: a URL-encoded data URI. Instead of base64 it keeps the markup readable and escapes only the characters that break a URL or a CSS url(): percent, hash, angle brackets, double quotes and question marks. It is not always shorter. A 136-character circle icon becomes 210 characters in base64 but 201 in URL-encoded form, because every double quote turns into three characters; SVG with few quotes and long path data gains more.

Decoding accepts a complete data URI (base64 or the percent-encoded form) or bare base64. Spaces and line breaks are removed first, because base64 copied from an email or a PDF often has them. The alphabet is A-Z, a-z, 0-9, + and /, with = padding at the end. The URL-safe variant that uses - and _ is reported with the substitution to make, rather than guessed at.

How it works

A chosen file larger than 10 MB is refused. The bytes are read, the first bytes are matched against PNG, JPEG, GIF and WEBP signatures, and failing that the opening of the text is tested for an svg element. Anything else is rejected with a message that says so. If you do not set a width or a format, the original bytes are encoded untouched, so the result decodes back to the identical file.

When you set a maximum width or choose an output format, the image is decoded by the browser (with EXIF orientation applied), drawn at the new size in halving steps for quality, and re-encoded. A JPG output fills transparency with white, JPG and WEBP use quality 85, and a GIF contributes only its first frame. If the browser cannot write WEBP, the page falls back to PNG and says so. Resizing never enlarges: an image narrower than your limit keeps its width.

Decoding strips whitespace, then looks at each character in turn. The first character outside the base64 alphabet is reported with its position, a misplaced = is reported separately, and a length that cannot be valid is explained. The decoded bytes are matched against the same signatures to find the real type. If the data URI claimed another type, the page tells you and saves the file with the real one.

Common use cases

  • Inlining a small logo or icon in an HTML email template or a single-file HTML report
  • Embedding a tiny background texture in a stylesheet to save one request
  • Putting a test image into a JSON fixture or an API request body
  • Recovering an image from a data URI found in a page's source, a database field or a log
  • Checking whether text copied from a document is valid base64 before debugging a bigger problem

How to use this tool

  1. Pick the Image to Base64 tab and choose an image, or switch to Base64 to image to paste text.
  2. For a raster image, optionally enter a maximum width and an output format to make the text shorter.
  3. Read the length and the percentage increase; a notice appears above 100 KB.
  4. Copy the data URI or the snippet that matches where it will go: HTML, CSS, Markdown or JSON.
  5. To go the other way, paste the data URI, check the detected type, view the picture and press the download button.

Example

pixel.png, a 1 × 1 red PNG (70 bytes)

70 bytes beginning 89 50 4E 47 0D 0A 1A 0A

Data URI, 118 characters (+69% over the file)

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/q842iQAAAABJRU5ErkJggg==

HTML:     <img src="data:image/png;base64,iVBORw0KGgo..." alt="pixel">
CSS:      background-image: url("data:image/png;base64,iVBORw0KGgo...");
Markdown: ![pixel](data:image/png;base64,iVBORw0KGgo...)
JSON:     {"image":"data:image/png;base64,iVBORw0KGgo..."}

Tips for best results

  • Keep inlined images small. Past a few kilobytes the extra length and lost caching usually cost more than a separate file would.
  • Resize before encoding: halving an image's width cuts its pixels to a quarter, and the text shrinks with it.
  • For flat graphics such as icons and logos, PNG often beats JPG; for photos, JPG or WEBP gives much shorter text.
  • Prefer the SVG URL-encoded form inside CSS, where it stays readable, and compare the two lengths before choosing.
  • When you paste base64 from a wrapped source, include the whole text; the page ignores the line breaks.

Edge cases to watch

A data URI cannot be cached separately from the document that holds it. If the same image is repeated across pages, every page carries its own copy.

Base64 is encoding, not protection. Anyone with the text can turn it back into the image.

Re-encoding a JPG or WEBP loses a little quality each time. Encode the original bytes (no width, format set to keep) when exactness matters.

A GIF resized or converted loses its animation because only the first frame is drawn.

Some tools limit the length of a URL or an attribute value, and very long data URIs may be truncated when pasted into a form or a chat message. Check the pasted result is complete.

An SVG that references external files or scripts is encoded as written. External references usually will not load from inside an image, and scripts do not run there.

Common errors and how to fix them

Invalid character "$" at position 4. Base64 uses only A-Z, a-z, 0-9, + and /.
Something other than a base64 character was pasted. Remove the character and any surrounding quote marks, or copy the original text again. If the character is - or _, replace it with + or / because the text uses the URL-safe alphabet.
The length is 23, not a multiple of 4. Add 1 "=" at the end if the text is complete.
Base64 text is padded with = so its length divides by 4. If the text was copied in full, add the padding the message names. If the message says part of the text was probably cut off, paste it again in full.
The decoded bytes are not a PNG, JPEG, WEBP, GIF or SVG image.
The text is valid base64, but what it holds is something else, for example a PDF or a text file. Check where it came from, or use the type selector only if you know the bytes belong to an image format.
"... is 12.4 MB; this tool accepts up to 10 MB."
Shrink the image first with the image compressor or resize it, then encode it. A data URI that large would also be impractical to paste.

Best practices

  • Encode the smallest version of the image that looks right at the size it will be shown.
  • Keep the original file; a data URI is hard to edit by hand.
  • Test the snippet in the page, stylesheet or document where it will live before relying on it.
  • For many images, link them as files so the browser can cache and load them in parallel.

Why the decoder trusts the bytes over the header

A data URI names its type before the comma, but that label is only a claim. A file saved with a wrong label still displays in some places and not in others, and a label written by hand is easy to get wrong. The decoder therefore reads the opening bytes of the data it recovers and uses them to choose the file extension. When the label and the bytes disagree it says so, so you can fix whatever produced the string.

If the bytes match none of the five supported formats, the type selector lets you force a type for preview and download. That is for cases such as a truncated image whose first bytes survived but whose header you cannot confirm. If the browser cannot draw the forced result, the page still offers the download.

Frequently asked questions

Who does the encoding and decoding?

Your browser tab does, with its built-in btoa and atob functions and a canvas for resizing. Neither the image nor the pasted text is placed in a request, and the result lives in page memory until you close or reload. The site's analytics run separately.

Why is the data URI longer than my file?

Base64 writes 3 bytes as 4 characters, which adds about a third, and the data:image/...;base64, prefix adds a fixed amount. For a very small file the prefix makes the increase look larger.

Can I decode base64 without the data: prefix?

Yes. Paste the raw base64 and the page detects the image type from the decoded bytes.

Which snippet should I use for a CSS file?

Use the CSS background-image snippet. It wraps the URI in quotes inside url(), with any quote or backslash in it escaped.

Can I turn a decoded SVG back into a file?

Yes. Paste the base64 or the URL-encoded data URI; when the bytes begin with an svg element the page previews it and saves decoded-image.svg.