Skip to content

Why File Size Increases by 33% with Base64 Encoding

Category: Encoding
This article is currently available in Japanese only. We are working on translations.

Have you ever heard that 「file size increases by about 33% when encoded in Base64」? Why is this? This article explains how Base64 encoding works, the mathematical reasons for file size increase, and its impact in development environments.

What is Base64

Base64 is an encoding scheme that represents binary data using only ASCII characters. It is used for transmitting binary data over text-only protocols, such as email attachments (MIME), data URI schemes, JWT tokens, and Basic authentication headers.

The characters used are <code>A–Z</code> (26 characters), <code>a–z</code> (26 characters), <code>0–9</code> (10 characters), <code>+</code>, <code>/</code> for a total of 64 characters (= Base<strong>64</strong>), plus <code>=</code> for padding.

Why does it increase by 33%

Base64 <strong>converts 3 bytes of binary into 4 ASCII characters</strong>.

  • 3 bytes = 24 bits
  • 24 bits ÷ 6 bits = 4 Base64 characters (each character represents 6 bits)

In other words, the original data becomes <strong>3 bytes → 4 characters</strong>. The size ratio is <code>4 ÷ 3 ≈ 1.333...</code>, so it <strong>increases by approximately 33.3%</strong>.

元バイナリ:   | 0x4D | 0x61 | 0x6E |  ← 3バイト
              01001101 01100001 01101110
              ↓ 6ビットずつに分割
              010011 010110 000101 101110
              ↓ Base64文字に変換
                 T       W       F       u   ← 4文字(= 4バイト)

Calculation of concrete size increase

Original file sizeSize After Base64Increase amount
1 MB(1,000,000 B)Approximately 1.333 MB+333 KB
10 MBApproximately 13.33 MB+3.33 MB
25 MB (Gmail limit)Approximately 33.3 MB+8.33 MB
100 MBApproximately 133.3 MB+33.3 MB

The exact formula is as follows.

import math

def base64_size(original_bytes: int) -> int:
    """Base64エンコード後のバイト数を計算"""
    # 3バイトを4文字に変換、4の倍数にパディング
    return math.ceil(original_bytes / 3) * 4

original = 10_000_000  # 10 MB
encoded  = base64_size(original)
print(f"元: {original:,} B")
print(f"後: {encoded:,} B")
print(f"増加率: {encoded/original*100:.1f}%")
# → 元: 10,000,000 B
# → 後: 13,333,336 B
# → 増加率: 133.3%

Additional increase due to line break characters

The MIME standard (email) requires that Base64 data be broken into lines every 76 characters with <code>\r\n</code> (CRLF). These line breaks also add extra bytes, slightly increasing the overall size.

def base64_mime_size(original_bytes: int, line_length: int = 76) -> int:
    """MIME形式(76文字改行)のBase64サイズを計算"""
    b64_chars = math.ceil(original_bytes / 3) * 4
    line_count = math.ceil(b64_chars / line_length)
    return b64_chars + line_count * 2  # CRLF = 2バイト/行

Development impact

File Transfer via API

When sending a file included in a JSON body via REST API, Base64 encoding is required.

// ファイルをBase64に変換してJSONで送る
const file = document.getElementById('file').files[0];
const reader = new FileReader();
reader.onload = (e) => {
    const base64 = e.target.result; // "data:image/png;base64,iVBORw0KGgo..."
    fetch('/api/upload', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ file: base64, name: file.name })
    });
};

In this case, the API request size limit is determined by the size <em>after</em> Base64 encoding. Note that when sending a 10MB file, the JSON body will be approximately 13.3MB or larger.

Email Attachment (SMTP)

Because the SMTP protocol is ASCII text-based, attachments are Base64-encoded. Gmail's attachment limit is 25 MB, which refers to the file size before encoding (Gmail handles the internal encoding). However, when sending directly via SMTP, the size limit applies to the encoded size, so caution is required.

Data URI for img Tag

<!-- Base64埋め込み画像 -->
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." />

When embedding images inline in HTML, the HTML file becomes larger by the Base64 size of the image. It is generally recommended to avoid inline embedding of large images as it affects page load speed.

Decode Base64 and Restore to Original Size

Summary

  • Base64 converts <strong>3 bytes → 4 characters</strong>, causing size to increase by <strong>approximately 33.3%</strong>
  • Further Slight Increase When MIME Line Breaks (Every 76 Characters) Are Present
  • For APIs That Send Files via JSON Body, the Request Limit is Determined by Post-Base64 Size
  • Base64 encoding is not required for HTML forms with <code>multipart/form-data</code> (binary is sent as-is)

Test files for this article

  • → <a href="/ja/files/images/png/" class="text-primary-600 dark:text-primary-400 hover:underline">PNG Image Test Files List</a>
  • → <a href="/ja/files/pdf/" class="text-primary-600 dark:text-primary-400 hover:underline">PDF Test File List</a>

Related articles

  • → <a href="/ja/blog/multipart-form-data-overhead/" class="text-primary-600 dark:text-primary-400 hover:underline">Calculating multipart/form-data Overhead Accurately</a>
  • → <a href="/ja/blog/file-format-quick-reference/" class="text-primary-600 dark:text-primary-400 hover:underline">File Format Quick Reference for Developers</a>