การเข้ารหัส
Data URI / รูปภาพ Base64
เข้ารหัสรูปภาพเป็น Data URI (data:image/…;base64,…) เพื่อฝังใน CSS/HTML และถอดรหัส Data URI หรือ Base64 ดิบกลับเป็นไฟล์ — ทั้งหมดในเบราว์เซอร์โดยตรง
ตัวอย่างรูปภาพจะแสดงที่นี่
A Data URI embeds a file's contents (usually an image) directly into HTML or CSS as a Base64 string, instead of linking to a separate file. This removes an extra network request at the cost of a larger document.
How to use it
- Encode: upload an image and the tool generates a ready data:image/…;base64,… string to paste into src, href, or CSS.
- Decode: paste a Data URI or raw Base64 to get the original file back for download.
- The copied string can be pasted straight into an HTML attribute or a CSS property with no extra processing.
Common uses
- Embedding small icons or sprites in CSS to avoid an extra HTTP request.
- Building email templates, where external images can get blocked by the email client.
- Quickly previewing an image as a self-contained string without uploading the file anywhere.
Things to keep in mind
Base64 encoding grows the data by about 33% — for large images, a Data URI is usually a worse choice than a separate file with browser caching.
A Data URI isn't cached separately from the document it's embedded in — it gets re-downloaded along with the page every time.
บทความเกี่ยวกับเครื่องมือนี้: Data URI: เมื่อไหร่ควรฝังรูปภาพลงในโค้ดโดยตรง
คำถามที่พบบ่อย
ควรใช้ Data URI แทนไฟล์ภาพปกติเมื่อไหร่?
Data URI เหมาะกับภาพขนาดเล็กที่ใช้บ่อย เช่น ไอคอนหรือโลโก้ เมื่อต้องการหลีกเลี่ยงการร้องขอ HTTP เพิ่มเติม เช่น ฝังภาพลงใน CSS หรือ HTML โดยตรง
การใช้ Data URI กับรูปภาพมีข้อเสียไหม?
มี มันทำให้ขนาดไฟล์โดยรวมใหญ่ขึ้น (ใหญ่กว่าภาพต้นฉบับราว 33% เพราะ Base64) ไม่ถูกแคชแยกต่างหากเหมือน URL ภาพจริง และเบราว์เซอร์หรือโปรแกรมอีเมลหลายตัวก็จำกัดขนาดของ Data URI ไว้
เครื่องมือนี้อัปโหลดภาพของฉันไปที่ไหนหรือไม่?
ไม่ ภาพถูกอ่านและเข้ารหัสทั้งหมดในเบราว์เซอร์ของคุณ ไม่เคยถูกส่งไปยังเซิร์ฟเวอร์
ใช้ Data URI สำหรับ SVG โดยไม่ใช้ Base64 ได้ไหม?
ได้ SVG คือข้อความ (XML) ดังนั้นแทนที่จะใช้ Base64 เพียงแค่ percent-encoding อักขระพิเศษก็เพียงพอ: data:image/svg+xml,%3Csvg...%3E วิธีนี้กระชับกว่าและยังคงแก้ไขได้โดยตรงในไฟล์ CSS
ใส่ Data URI ในแอตทริบิวต์ href หรือ background-image ได้ไหม?
ได้ Data URI ใช้งานได้ทุกที่ที่คาดว่าจะมีลิงก์อ้างอิงถึงทรัพยากร — ใน src, href, background-image ใน CSS และแม้แต่ใน @import ไวยากรณ์และข้อจำกัดขนาดเหมือนกับที่ใช้กับแท็ก img