All files / toolbox / imgb64.ts

100.00% Branches 2/2
100.00% Functions 1/1
100.00% Lines 8/8
1
2
3
4
5
6
7
8
9
10
x1
 
 
x1
x2
x2
x1
x1
x1
x2







import { encodeBase64 } from "@std/encoding/base64"

/** Download a remote PNG and return it as a base64 data URL. */
export async function imgb64(url: string): Promise<string> {
  try {
    return `data:image/png;base64,${encodeBase64(await fetch(url).then((res) => res.arrayBuffer()))}`
  } catch {
    return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJggg=="
  }
}