Base64图片编码器
将图片编码为 Base64 Data URI,用于 HTML 和 CSS
将图片拖放到此处或点击上传
支持 PNG、JPG、SVG、WebP、GIF、AVIF
什么是 Base64 图片编码?
Base64 图片编码将二进制图片数据转换为 ASCII 字符串,可直接嵌入 HTML、CSS 或 JSON,无需引用外部文件。结果是一个 data URI——一个以 data:image/png;base64, 开头、后接图片 Base64 编码字节的自包含字符串。浏览器处理 data URI 的方式与外部图片 URL 完全相同:解码 Base64 内容并内联渲染图片。
该技术由 RFC 2397(data URI 方案)和 RFC 4648(Base64 编码)定义。图片经 Base64 编码后,每 3 个字节的二进制像素数据转换为 4 个 ASCII 字符,体积增加约 33%。尽管存在这一开销,内联图片可消除每个资源的 HTTP 请求——对于小型图片(图标、Logo、1×1 追踪像素)而言,单次请求的往返延迟往往超过额外字节的代价,因此这是一项有意义的优化。
所有现代浏览器、电子邮件客户端(有一定限制)以及任何支持标准 URI 语法的环境均支持 data URI。它广泛应用于单页应用、HTML 邮件模板、SVG 精灵图以及 CSS background-image 声明中,通过减少网络请求总数来改善感知加载速度。
为什么使用这个 Base64 图片编码器?
本工具可直接在浏览器中将任意图片转换为可即用的 data URI——无需上传、无需服务器、无需账号。
Base64 图片编码使用场景
Data URI 结构
data URI 将图片格式和二进制内容编码在一个字符串中。了解其结构有助于以编程方式构建或解析 data URI:
| 组成部分 | 说明 |
|---|---|
| data: | URI scheme identifier |
| image/png | MIME type of the image |
| ;base64, | Encoding declaration |
| iVBORw0KGgo... | Base64-encoded binary data |
Base64 编码图片格式对比
图片格式的选择会影响 Base64 输出大小、浏览器支持和渲染特性。请根据内容类型选择最合适的格式:
| 格式 | MIME 类型 | 透明度 | 适用场景 |
|---|---|---|---|
| PNG | image/png | Yes | Screenshots, icons, UI elements |
| JPEG | image/jpeg | No | Photos, gradients, large images |
| SVG | image/svg+xml | Yes | Logos, icons, scalable graphics |
| WebP | image/webp | Yes | Modern web images, smaller files |
| GIF | image/gif | Yes (1-bit) | Simple animations, small icons |
| ICO | image/x-icon | Yes | Favicons |
代码示例
如何在主流语言和环境中将图片转换为 Base64 data URI:
// Convert a File object to a Base64 data URI
function fileToDataUri(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => resolve(reader.result)
reader.onerror = reject
reader.readAsDataURL(file)
// reader.result → "data:image/png;base64,iVBORw0KGgo..."
})
}
// Convert a canvas element to a data URI
const dataUri = canvas.toDataURL('image/png')
// → "data:image/png;base64,iVBORw0KGgo..."
// Convert with quality parameter (JPEG/WebP only)
const jpegUri = canvas.toDataURL('image/jpeg', 0.8)
// → "data:image/jpeg;base64,/9j/4AAQ..."import base64
# Read an image file and encode it to a data URI
with open('logo.png', 'rb') as f:
encoded = base64.b64encode(f.read()).decode('ascii')
data_uri = f'data:image/png;base64,{encoded}'
# → "data:image/png;base64,iVBORw0KGgo..."
# Detect MIME type automatically
import mimetypes
mime, _ = mimetypes.guess_type('photo.jpg')
# mime → "image/jpeg"
with open('photo.jpg', 'rb') as f:
b64 = base64.b64encode(f.read()).decode('ascii')
data_uri = f'data:{mime};base64,{b64}'import { readFileSync } from 'fs'
import { lookup } from 'mime-types'
// Read file and encode to data URI
const buffer = readFileSync('icon.png')
const base64 = buffer.toString('base64')
const mime = lookup('icon.png') // → "image/png"
const dataUri = `data:${mime};base64,${base64}`
// → "data:image/png;base64,iVBORw0KGgo..."
// From a Buffer received via HTTP/API
function bufferToDataUri(buf, mimeType) {
return `data:${mimeType};base64,${buf.toString('base64')}`
}# Encode an image to Base64 (macOS) base64 -i logo.png | tr -d '\n' # Encode an image to a full data URI (Linux) echo -n "data:image/png;base64,$(base64 -w 0 logo.png)" # Encode and copy to clipboard (macOS) echo -n "data:image/png;base64,$(base64 -i logo.png | tr -d '\n')" | pbcopy # Detect MIME type and encode (Linux, requires file command) MIME=$(file -b --mime-type image.webp) echo -n "data:$MIME;base64,$(base64 -w 0 image.webp)"