Skip to content

API Reference

Detects an image MIME type from magic bytes. Returns null if the bytes don’t match a known signature.

param type required
bytes Uint8Array | ArrayBuffer yes

Returns: "image/png" \| "image/jpeg" \| "image/gif" \| "image/webp" \| "image/avif" \| null

import { sniffMimeType } from "edge64";
const mime = sniffMimeType(bytes);
// "image/png"

Fetches an image from a URL and returns its base64 encoding, data URI, and detected MIME type. Prefers a valid content-type response header; falls back to magic-byte sniffing if the header is missing or not an image/* type.

param type default required
url string — yes
init RequestInit undefined no

Returns: Promise<{ mime: string, base64: string, dataUri: string }>

Throws if the fetch response is not ok, or if no MIME type could be determined.

import { imageUrlToBase64 } from "edge64";
const { mime, base64, dataUri } = await imageUrlToBase64("https://example.com/cat.png");

Decodes a base64 string or a full data: URI back into bytes.

param type required
input string yes

Returns: Uint8Array

import { base64ToBytes } from "edge64";
const bytes = base64ToBytes("data:image/png;base64,iVBORw0KGgo...");
// or
const bytes2 = base64ToBytes("iVBORw0KGgo...");

Builds a data: URI from raw bytes. If mimeHint is omitted, the MIME type is detected with sniffMimeType.

param type default required
bytes Uint8Array | ArrayBuffer — yes
mimeHint string undefined no

Returns: string

Throws if no mimeHint is given and the MIME type can’t be sniffed.

import { bytesToDataUri } from "edge64";
const uri = bytesToDataUri(bytes);
// "data:image/png;base64,..."