Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 73a299d

Browse files
committed
feat: add package for performant base64 encoding
1 parent 5d3ba91 commit 73a299d

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
},
4444
"homepage": "https://github.com/image-js/image-js#readme",
4545
"dependencies": {
46+
"@jsonjoy.com/base64": "^1.1.2",
4647
"bresenham-zingl": "^0.2.0",
4748
"colord": "^2.9.3",
4849
"fast-bmp": "^2.0.1",

src/save/__tests__/encodeBase64.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { createRgbaImage } from '../../../test/testUtils.js';
22
import { encode } from '../encode.js';
33
import { encodeBase64 } from '../encodeBase64.js';
44

5+
/**
6+
* @vitest-environment jsdom
7+
*/
58
test('basic image (png)', () => {
69
const image = testUtils.createGreyImage([
710
[0, 0, 0, 0, 0],

src/save/encodeBase64.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { toBase64 } from '@jsonjoy.com/base64';
2+
13
import type { Image } from '../Image.js';
24

35
import { encode } from './encode.js';
46
import type { ImageFormat } from './encode.js';
5-
67
/**
78
* Converts image into a base64 URL string.
89
* @param image - Image to get base64 encoding from.
@@ -11,12 +12,7 @@ import type { ImageFormat } from './encode.js';
1112
*/
1213
export function encodeBase64(image: Image, format: ImageFormat) {
1314
const buffer = encode(image, { format });
14-
const binaryArray = [];
15-
for (const el of buffer) {
16-
binaryArray.push(String.fromCodePoint(el));
17-
}
18-
const binaryString = binaryArray.join('');
19-
const base64String = btoa(binaryString);
15+
const base64String = toBase64(buffer);
2016
const dataURL = `data:image/${format};base64,${base64String}`;
2117
return dataURL;
2218
}

0 commit comments

Comments
 (0)