How to convert an image to Base64

  1. Drop an image onto the box above, click it to choose a file, or copy an image and paste it with Ctrl+V (⌘V on a Mac).
  2. Pick the output you need, then click Copy.

The output options:

  • Data URI: data:image/png;base64,…, ready for an src attribute or a CSS url().
  • Base64 only: just the encoded text, for JSON fields and APIs that ask for the raw Base64.
  • HTML <img>: a complete image tag.
  • CSS: a background-image declaration.
  • Markdown: an image you can paste into a README or a note.

The tool also shows the image's type, dimensions, file size and the length of the result. It works with PNG, JPG, GIF, WebP, SVG, ICO, BMP and AVIF. The file is read in your browser, so it is never uploaded.

To go the other way, switch to Base64 → Image, or open the Base64 to Image converter.

What an image data URI looks like

A data URI puts the whole image inside the text of your page:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==

  • data: says that the data follows directly instead of a web address.
  • image/png is the file type. It must match the image, or some browsers will not show it.
  • ;base64, says that what follows is Base64.
  • Everything after the comma is the image file itself, encoded as text.

When to embed an image as Base64

Base64 is a good fit for:

  • Small icons and logos in HTML email, where linked images are often blocked until the reader allows them.
  • Single-file pages, demos and offline HTML reports that must work without separate image files.
  • CSS for small backgrounds and icons, so the page needs one request fewer.
  • JSON and APIs that accept an image as a Base64 string, such as many AI vision and OCR services.

It is a poor fit for large photos. Base64 makes the data about 33% larger, the browser cannot cache the image separately, and the page cannot start drawing until the whole string has loaded. As a rule of thumb, keep embedded images under about 100 KB, and compress the image first if it is larger.

SVG: Base64 or plain text?

SVG files are already text, so a data URI can hold them without Base64 by URL-encoding a few characters, for example data:image/svg+xml,%3Csvg …%3E. That result is often smaller than Base64. Base64 is still the safer choice when the SVG goes into an attribute or a CSS file where quotes and # signs would cause trouble, and it is what this tool produces for every format.

Convert an image to Base64 in code

  • JavaScript (browser): read the file with FileReader.readAsDataURL(file), which returns the data URI.
  • Node.js: fs.readFileSync('logo.png').toString('base64')
  • Python: base64.b64encode(open('logo.png', 'rb').read()).decode()
  • PHP: base64_encode(file_get_contents('logo.png'))
  • Command line (macOS, Linux): base64 -i logo.png on macOS, base64 -w 0 logo.png on Linux.

Frequently asked questions

Is my image uploaded?

No. The image is read and encoded by your browser. Nothing is sent to QuickToolsBox or anyone else, so the tool also works for private screenshots and documents.

Why is the Base64 bigger than the image?

Base64 writes every 3 bytes of the file as 4 text characters, so the result is about 33% larger than the image file. A 30 KB PNG becomes roughly 40 KB of text.

Should I use the data URI or Base64 only?

Use the Data URI wherever a browser shows the image: an <img src>, a CSS url(), or Markdown. Use Base64 only when an API or a JSON field asks for the image as Base64 and sets the type separately.

Can I convert several images at once?

This tool converts one image at a time, so the result is easy to copy. Drop the next image to replace it.

Related tools