Advertisement

Base64 to Image Converter

Convert Base64 strings to images with instant live preview, download as PNG, JPG, or WebP, or copy the image directly to your clipboard. Converts both ways — Base64 to Image and Image to Base64.

Base64 String or Data URI
0 characters
Image Preview

Paste a Base64 string on the left to render preview

What is Base64 to Image Conversion?

Base64 is a standard binary-to-text encoding mechanism defined by RFC 4648. It takes binary data—such as image bytes representing PNG, JPEG, SVG, WebP, or GIF files—and translates it into a string of ASCII characters using a set of 64 characters (A-Z, a-z, 0-9, +, /, with = for padding).

Converting Base64 to Image decodes this text representation back into binary graphics that browsers, operating systems, and image viewers can display and save. By embedding images as Base64 Data URIs directly into HTML, CSS, JSON, or email messages, developers can eliminate external HTTP requests and bundle images inline.

The Structure of an Image Data URI

A standard Base64 image string is formatted as a Data URL:

data:[<mediatype>][;base64],<data>
  • data: — The URI scheme identifier.
  • image/png, image/jpeg, image/webp, image/svg+xml — The MIME type defining the format.
  • ;base64, — Indicates that the payload data is Base64 encoded.
  • <data> — The Base64 encoded binary bytes of the image file.

How to Convert Base64 to Image in Code

Need to decode Base64 into an image in your application? Here are ready-to-use code snippets across common programming languages:

1. JavaScript (Browser)

// Convert Base64 string to an HTMLImageElement
function base64ToImage(base64String) {
    return new Promise((resolve, reject) => {
        const img = new Image();
        img.onload = () => resolve(img);
        img.onerror = reject;
        img.src = base64String.startsWith('data:') 
            ? base64String 
            : `data:image/png;base64,${base64String}`;
    });
}

// Convert Base64 string to a Blob for download or uploading
function base64ToBlob(base64String, mimeType = 'image/png') {
    const raw = base64String.replace(/^data:[^;]+;base64,/, '');
    const byteCharacters = atob(raw);
    const byteNumbers = new Uint8Array(byteCharacters.length);
    for (let i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
    }
    return new Blob([byteNumbers], { type: mimeType });
}

2. Node.js

const fs = require('fs');

function saveBase64Image(base64String, outputPath) {
    // Strip data URI prefix if present
    const base64Data = base64String.replace(/^data:image\/\w+;base64,/, '');
    const buffer = Buffer.from(base64Data, 'base64');
    fs.writeFileSync(outputPath, buffer);
    console.log(`Saved image to ${outputPath}`);
}

3. Python 3

import base64
import re

def base64_to_image(b64_string, output_filename='output.png'):
    # Remove data:image/...;base64, header if present
    data = re.sub('^data:image/.+;base64,', '', b64_string)
    image_bytes = base64.b64decode(data)
    with open(output_filename, 'wb') as f:
        f.write(image_bytes)

# Usage
base64_to_image("iVBORw0KGgo...", "photo.png")

4. PHP

<?php
function base64ToImage($base64String, $outputFile) {
    $data = explode(',', $base64String);
    $binaryData = base64_decode(end($data));
    file_put_contents($outputFile, $binaryData);
}
?>

Common Use Cases

🚀 Web Performance & Critical CSS

Embedding small icons and splash logos directly in CSS avoids additional round-trip HTTP requests, reducing first contentful paint (FCP) time.

✉️ HTML Email Newsletters

Embedding brand logos as inline Data URIs prevents email clients from blocking remote images or showing broken image placeholder boxes.

📡 REST APIs & WebSockets

When transmitting image snapshots or signatures through JSON payloads where multipart uploads are inconvenient, Base64 is the industry standard.

🎨 Canvas & Screenshot Exports

HTML5 Canvas exports drawings and chart graphics via canvas.toDataURL() as Base64 images for quick downloading or sharing.

Frequently Asked Questions (FAQ)

Why is a Base64 image 33% larger than the original binary file?

Base64 maps every 3 bytes (24 bits) of raw binary data into 4 characters (32 bits) chosen from its 64 ASCII character alphabet. This represents a 4:3 expansion ratio, making the Base64 representation roughly 33% larger in file size than the original binary image.

What image formats are supported?

Our converter supports all modern raster and vector image formats: PNG (image/png), JPEG/JPG (image/jpeg), WebP (image/webp), GIF (image/gif), SVG (image/svg+xml), BMP (image/bmp), and ICO (image/x-icon).

Is my data private and secure?

Yes, absolutely. The conversion process is executed strictly in your web browser using HTML5 client-side JavaScript APIs. Your images and encoded Base64 strings are never uploaded to any remote server, stored, or analyzed.