🔡

Image to Base64

Select an image and convert it into a Base64 data URL locally.

preview

Image to Base64 — what it does

Convert an image into a Base64 string, entirely in the browser, ready to paste into CSS, HTML or JSON as a data URI. Embedding a small image this way removes one network request from a page — which is why icons, spinners and tiny logos are so often inlined rather than fetched.

Worked example

A 16 × 16 pixel list-bullet icon, bullet-16x16.png (86 bytes), converted; the output is the complete data URI and is shown here in full.

Input — bullet-16x16.png

bullet-16x16.png — 16 × 16 px, 1-bit grayscale PNG, 86 bytes

Output — data URI

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQAAAAA3iMLMAAAAHUlEQVR42mP4/58Bgn7IM3zgZ3jAjo6AgkApmDIACYMVi36pwpsAAAAASUVORK5CYII=

The 86-byte file becomes a 138-character string: the 22-character data:image/png;base64, prefix plus 116 characters of Base64.

When this tool helps

  • Inlining a small icon or logo in CSS so it loads with the stylesheet.
  • Embedding an image inside an HTML email template with no external hosting.
  • Passing image data through a JSON API that accepts Base64 fields.

How to use it

  1. Choose the image file.
  2. Let the tool encode it.
  3. Copy the Base64 output.
  4. Paste it into your CSS, HTML or JSON with the appropriate data URI prefix.

Accuracy, limits, and good practice

  • Keep it small. Base64 adds about a third to the byte count, so inlining anything above a few kilobytes usually costs more than the saved request.
  • For CSS, the value takes the form url(data:image/png;base64,...) — the tool provides the encoded part.
  • Inlined images cannot be cached separately, so a Base64 image embedded in HTML is re-downloaded with every page load.

Frequently asked questions

When should I not inline an image?

For anything reused across pages, or larger than a few kilobytes. Those benefit from being cached as separate files.

Does inlining work in email?

Support is mixed; several major clients block or ignore data URIs, so test before relying on it for a campaign.

Can I decode it back to a file?

Yes, the file Base64 tool decodes a string back into a downloadable image.

Does the output include the data: prefix?

Yes. The string starts with data:<mime type>;base64, using the type of the file you chose, so it can go straight into an img src or a CSS url(). Delete everything up to and including the comma if you need the bare Base64.

How long will the string be for a normal image?

About a third longer than the file, all on one line: the 122 KB logo-400x400.png gives a 167,262-character string. The box shows it in full, so for anything beyond a few kilobytes copy it rather than reading it.

Related tools on this site

Everything above runs inside this page. Image to Base64 needs no account, no upload, and no server round trip — close the tab and nothing is left behind.

圖片轉 Base64能做什麼

完全在瀏覽器中把圖片轉成 Base64 字串,可直接以 data URI 貼進 CSS、HTML 或 JSON。用這種方式內嵌小圖,可以為頁面省下一次網路請求——這也是圖示、載入動畫與小型商標常被內嵌而非另外抓取的原因。

實際範例

一個 16 × 16 像素的清單圓點圖示 bullet-16x16.png(86 bytes)轉換後的結果;輸出是完整的 data URI,此處完整列出。

輸入:bullet-16x16.png

bullet-16x16.png — 16 × 16 px, 1-bit grayscale PNG, 86 bytes

輸出:data URI

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQAAAAA3iMLMAAAAHUlEQVR42mP4/58Bgn7IM3zgZ3jAjo6AgkApmDIACYMVi36pwpsAAAAASUVORK5CYII=

86 bytes 的檔案變成 138 個字元:22 個字元的 data:image/png;base64, 前綴,加上 116 個 Base64 字元。

什麼時候用得上

  • 把小圖示或商標內嵌進 CSS,隨樣式表一起載入。
  • 在不需外部空間的情況下,把圖片嵌進 HTML 郵件範本。
  • 透過接受 Base64 欄位的 JSON API 傳遞影像資料。

使用步驟

  1. 選擇圖片檔案。
  2. 由工具完成編碼。
  3. 複製 Base64 輸出。
  4. 加上對應的 data URI 前綴,貼進 CSS、HTML 或 JSON。

精確度、限制與實務建議

  • 務必小。Base64 會讓位元組數增加約三分之一,超過幾 KB 的內容內嵌後,代價通常大於省下的那次請求。
  • 在 CSS 中形式為 url(data:image/png;base64,...)——工具提供的是編碼本體。
  • 內嵌圖片無法單獨快取,因此嵌在 HTML 裡的 Base64 圖片每次載入頁面都會重新下載。

常見問題

什麼情況不該內嵌?

跨頁面重複使用、或大於幾 KB 的圖片。那些做成獨立檔案由快取處理更划算。

郵件裡可以內嵌嗎?

支援度不一,好幾家主流郵件軟體會封鎖或忽略 data URI,正式發送前請先測試。

可以解碼回檔案嗎?

可以,檔案 Base64 工具能把字串還原成可下載的圖片。

輸出有包含 data: 前綴嗎?

有。字串以 data:<MIME 類型>;base64, 開頭,類型取自你選的檔案,可直接放進 img 的 src 或 CSS 的 url()。若只要純 Base64,把逗號(含)之前的部分刪掉即可。

一般圖片轉出來的字串有多長?

大約比檔案本身長三分之一,而且全在同一行:122 KB 的 logo-400x400.png 會產生 167,262 個字元。輸出區會完整顯示,超過幾 KB 的圖片建議直接複製而不是用看的。

本站相關工具

以上步驟全部在這個頁面內完成,圖片轉 Base64 不需要註冊、不上傳檔案、也不經過伺服器,關閉分頁後不會留下任何資料。