Image to Base64 Converter

Published Updated

Turn an image into one line of Base64 text, or tick a box for a full data:image/...;base64, URL to paste into HTML or CSS. The text runs to 4 characters per 3 bytes, so a 30,000-byte icon gives 40,000 characters.

Bare Base64 or a data: URL

The tool reads the image with FileReader.readAsDataURL, which returns a complete data: URL, and by default shows only the part after the comma. Tick Include Data URL prefix and you get the full form that RFC 2397 defines, data:[<mediatype>][;base64],<data>, ready for markup.

The media type in that prefix is not read from the image bytes. MDN's File.type page notes that browsers take it from the file extension, so a PNG renamed photo.jpg is labelled image/jpeg.

The prefix each file name receives

Measured by picking one file under each name in a real file input, then reading it the way the tool does:

File name ends inGoogle Chrome 156Firefox 155WebKit 26.6
.pngimage/pngimage/pngimage/png
.jpg or .jpegimage/jpegimage/jpegimage/jpeg
.jfifimage/jpegimage/jpegapplication/octet-stream
.svgimage/svg+xmlimage/svg+xmlimage/svg+xml
.icoimage/vnd.microsoft.iconimage/x-iconimage/x-icon
.heicimage/heicimage/heicimage/heic
no extensionapplication/octet-streamapplication/octet-streamapplication/octet-stream

Where the string goes

  • HTML: <img src="data:image/png;base64,...">. The example in RFC 2397 itself is a small GIF inside an IMG tag.
  • CSS: background-image: url("data:image/svg+xml;base64,...").
  • JSON and most APIs: the bare string without the prefix, which is what the MDN page above says to keep when only Base64 is needed.

Encoding an image

  1. Step 1

    Add the image

    The page opens in File and Encode mode. Pick an image, or drop any file onto the box.

  2. Step 2

    Decide on the prefix

    Leave Include Data URL prefix unticked for the bare Base64, or tick it for a data: URL that starts with the image type.

  3. Step 3

    Process and copy

    Press Process. The string appears with its length in characters, and Copy to Clipboard puts it on your clipboard.

Size and limits

RFC 4648 encodes each 24-bit group of input as 4 characters, which is where the extra third comes from.

  • RFC 2397 calls the scheme useful only for short values and says its own example image is probably near the limit of usefulness. Small icons and logos are the sweet spot.
  • An image written into a page or stylesheet is not a separate file, so it cannot be cached on its own or shared by other pages; each page that holds it carries its own copy.
  • Browsers do cap length: MDN's data: URL reference gives 512 MB for Chromium and Firefox and 2048 MB for Safari.
  • Everything happens in memory, and the result sits in one scrolling box; in our test the 14,073,292-character string of a file the tool lists as 10.55 MB took about 1.9 seconds to appear in Chrome 156 and about 13 seconds in Playwright's WebKit build (not the Safari app); the timing table further down has the details.

Sixteen files encoded, then decoded back

We put generated test files into this tool's own file box in Chrome 156, Chromium 153 (Playwright build, not Google Chrome), Firefox 155 and the WebKit build. The browser gave each file its type from the name, as it does with a real file chooser. Each file was encoded without and with the prefix, the data: URL was pasted into the Decode side of the same tool, and the decoded file was compared with the original by SHA-256.

FileBytesBase64 charactersPrefix the tool wroteDecoded back
tiny.png178240data:image/png;base64,Same bytes, read as a PNG image
photo.jpg18,56724,756data:image/jpeg;base64,Same bytes
anim.gif (2 frames)6,3498,468data:image/gif;base64,Same bytes
pic.webp9881,320data:image/webp;base64,Same bytes
pic.bmp24,05432,072data:image/bmp;base64,Same bytes
icon.ico285380data:image/vnd.microsoft.icon;base64, in the two Chrome builds, data:image/x-icon;base64, in Firefox and WebKitSame bytes
vector.svg185248data:image/svg+xml;base64,Same bytes
pic.avif484648data:image/avif;base64,Same bytes
png-named.jpg (PNG bytes)178240data:image/jpeg;base64,Same bytes, saved as decoded.png with the warning The data URL says image/jpeg, but the bytes are a PNG image
jpeg-named.png (JPEG bytes)18,56724,756data:image/png;base64,Same bytes, saved as decoded.jpg with the matching warning
UPPER.PNG178240data:image/png;base64,Same bytes
A PNG with no extension178240data:application/octet-stream;base64,Same bytes, saved as decoded.png
doc.pdf329440data:application/pdf;base64,Same bytes, saved as decoded.pdf
Three noise PNGs1,054,390 / 10,554,969 / 26,382,3001,405,856 / 14,073,292 / 35,176,400data:image/png;base64,Same bytes, except the largest one in the WebKit build (see the timing notes)
empty.png0NoneNoneRefused: One or more files were empty (0 bytes) and couldn't be used

The four engines agreed on every row except the .ico prefix and the largest file in the WebKit build. Every string had 4 characters for each 3 bytes, rounded up, and none had a line break. The counter under the result shows plain digits, such as 35176400 characters. The prefix added 22 characters for a PNG and 37 for the generic application/octet-stream one. The PDF shows that the box encodes whatever it is given: the image filter only narrows what the file chooser offers.

The prefix follows the file name, not the bytes, so the two mislabelled files carried the wrong type in their data: URL. The Decode side caught both, because it reads the type from the first bytes.

How long large images take

Time from pressing Process until the Base64 string was on the page, one run per engine, for the noise PNGs above (random generated pixels):

Engine1,054,390 bytes10,554,969 bytes26,382,300 bytes
Chrome 156205 ms1,858 ms7,536 ms
Chromium 153205 ms1,642 ms4,021 ms
Firefox 155231 ms2,099 ms4,731 ms
The WebKit build877 ms13,419 ms131,481 ms (a separate run with this file alone)

Reading the file took at most 196 ms in every engine; the time goes into placing the long string on the page. Decoding the data: URL back took 22 to 39 ms for the file the tool lists as 1.05 MB and 276 to 377 ms for the 10.55 MB one. For the 26.38 MB file it took 612 to 2,230 ms in three engines. In the WebKit build, decoding that file back had not finished when the 10-minute limit of its run was reached; the first run with all 17 files had not finished this file within its 15-minute limit either.

Not tested: files above 26.38 MB (as the tool lists their size), real photos, the Copy to Clipboard button, and pasting the result into an HTML page or a stylesheet. Times depend on the device.

Image to Base64 FAQ

Should I tick Include Data URL prefix?

Tick it when the string goes straight into an src attribute or a CSS url(), which need the full data: URL. Leave it off for a JSON field or an API that asks for Base64 alone, and for any form that adds its own prefix. The box starts unticked.

Why does my data URL say application/octet-stream?

The type comes from the file name. A file with no extension, or one the browser does not recognise, has no type, so the generic label is used; in Safari a .jfif photo gets it too. Rename the file with the right extension and encode it again, or edit the start of the string by hand.

How long will the string be?

Four characters for every three bytes, rounded up, plus the prefix if you ask for it. A 4,183-byte PNG gives 5,580 characters, or 5,602 with data:image/png;base64, in front. A 10 MB photo gives about 13.3 million characters, which works but makes a heavy HTML or CSS file.

Does encoding change or shrink the image?

No. The file's bytes are copied exactly, so the string decodes to the same file, metadata included. A phone photo keeps its EXIF camera details and GPS position inside the Base64. Run it through EXIF Remover first if those should not travel.

Can I turn a Base64 string back into an image here?

Yes. Switch the action to Decode, paste the string or a whole data: URL, and press Process. A data: URL brings its own type; for a bare string the page reads the type from the first bytes, and the extension menu is the fallback. Base64 to Image is a page made just for that direction.

Is there a .txt download of the result?

Not in File mode: the result offers Copy to Clipboard only. The Download as .txt button belongs to Text mode, which encodes typed text rather than files.

Why does my PNG come out as data:image/jpeg?

The prefix follows the file name, not the bytes. In our test PNG bytes saved as png-named.jpg became data:image/jpeg;base64,... in the four browser engines we tried. Pasting that string into the Decode side brought the warning The data URL says image/jpeg, but the bytes are a PNG image, and the file was saved as .png. Renaming the file before encoding gives the right prefix.

Does the Base64 string have line breaks in it?

No. None of the test strings had a line break, including one of 35,176,400 characters. If a system wants the text wrapped into lines, the wrapping has to happen there.

How large an image can I convert?

The page sets no size limit of its own. A 26.38 MB file appeared as a 35,176,400-character string in 4.0 to 7.5 seconds in three of the four browser engines we tried, while the WebKit build took 13.4 seconds for a 10.55 MB file and about 2.2 minutes for the 26.38 MB one.

How can I check that the string turns back into the same image?

Switch the action to Decode in the same tool and paste the string: the result names the type it read from the first bytes, and the file can be downloaded. Every decoded test file that finished had the same SHA-256 as its original; in the WebKit build the decode of the 26.38 MB file had not finished when the test stopped.

Related Tools