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

Commit fea99ac

Browse files
committed
fix: resolve conversations
1 parent de882ba commit fea99ac

File tree

7 files changed

+40
-70
lines changed

7 files changed

+40
-70
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
"homepage": "https://github.com/image-js/image-js#readme",
4545
"dependencies": {
46-
"@jsonjoy.com/base64": "^1.1.2",
46+
4747
"bresenham-zingl": "^0.2.0",
4848
"colord": "^2.9.3",
4949
"fast-bmp": "^2.0.1",
@@ -94,6 +94,7 @@
9494
"rimraf": "^6.0.1",
9595
"tailwindcss": "^3.4.17",
9696
"typescript": "~5.7.3",
97+
"uint8-base64": "^0.1.1",
9798
"vite": "^6.0.8",
9899
"vitest": "^2.1.8"
99100
}

src/save/__tests__/encodeBase64.test.ts renamed to src/save/__tests__/encodeDataURL.test.ts

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { createRgbaImage } from '../../../test/testUtils.js';
21
import { encode } from '../encode.js';
3-
import { encodeBase64 } from '../encodeBase64.js';
2+
import { encodeDataURL } from '../encodeDataURL.js';
43

54
test('basic image (png)', () => {
65
const image = testUtils.createGreyImage([
@@ -10,7 +9,7 @@ test('basic image (png)', () => {
109
[0, 255, 255, 255, 0],
1110
[255, 0, 255, 0, 255],
1211
]);
13-
const base64Url = encodeBase64(image, 'png');
12+
const base64Url = encodeDataURL(image);
1413

1514
expect(base64Url).toBe(
1615
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAAAAACoBHk5AAAAFklEQVR4XmNggID///+DSCCEskHM/wCAnQr2TY5mOAAAAABJRU5ErkJggg==',
@@ -26,59 +25,22 @@ test('basic image 2 (jpeg)', () => {
2625
[255, 255, 255, 255, 255],
2726
]);
2827
const format = 'jpeg';
29-
const base64 = encodeBase64(image, format);
28+
const base64 = encodeDataURL(image, { format });
3029
const base64Data = Buffer.from(encode(image, { format })).toString('base64');
3130
expect(typeof base64).toBe('string');
3231
expect(base64Data).toMatchSnapshot();
3332
});
3433

3534
test('legacy image-js test', () => {
36-
const image = createRgbaImage([
37-
[
38-
255,
39-
0,
40-
0,
41-
255, // red
42-
0,
43-
255,
44-
0,
45-
255, // green
46-
0,
47-
0,
48-
255,
49-
255, // blue
50-
],
51-
[
52-
255,
53-
255,
54-
0,
55-
255, // yellow
56-
255,
57-
0,
58-
255,
59-
255, // magenta
60-
0,
61-
255,
62-
255,
63-
255, // cyan
64-
],
65-
[
66-
0,
67-
0,
68-
0,
69-
255, // black
70-
255,
71-
255,
72-
255,
73-
255, // white
74-
127,
75-
127,
76-
127,
77-
255, //grey
78-
],
79-
]);
35+
const image = testUtils.createRgbaImage(
36+
`
37+
255 0 0 / 255 | 0 255 0 / 255 | 0 0 255 / 255
38+
255 255 0 / 255 | 255 0 255 / 255 | 0 255 255 / 255
39+
0 0 0 / 255 | 255 255 255 / 255 | 127 127 127 / 255
40+
`,
41+
);
8042
const format = 'jpeg';
81-
const url = encodeBase64(image, format);
43+
const url = encodeDataURL(image, { format });
8244
const base64Data = Buffer.from(encode(image, { format })).toString('base64');
8345
expect(typeof url).toBe('string');
8446
expect(base64Data).toBe(url.slice(url.indexOf(',') + 1));

src/save/encode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface EncodeOptionsJpeg {
2929
export interface EncodeOptionsBmp {
3030
format: 'bmp';
3131
}
32-
const defaultPng: EncodeOptionsPng = { format: 'png' };
32+
export const defaultPng: EncodeOptionsPng = { format: 'png' };
3333

3434
/**
3535
* Encodes the image to the specified format

src/save/encodeBase64.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/save/encodeDataURL.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { encode as uint8encode } from 'uint8-base64';
2+
3+
import type { Image } from '../Image.js';
4+
5+
import type {
6+
EncodeOptionsBmp,
7+
EncodeOptionsJpeg,
8+
EncodeOptionsPng,
9+
} from './encode.js';
10+
import { encode, defaultPng } from './encode.js';
11+
/**
12+
* Converts image into Data URL string.
13+
* @param image - Image to get base64 encoding from.
14+
* @param options - Encoding options.
15+
* @returns base64 string.
16+
*/
17+
export function encodeDataURL(
18+
image: Image,
19+
options: EncodeOptionsBmp | EncodeOptionsPng | EncodeOptionsJpeg = defaultPng,
20+
) {
21+
const buffer = encode(image, options);
22+
const base64 = uint8encode(buffer);
23+
const base64Data = new TextDecoder().decode(base64);
24+
return `data:image/${options.format};base64,${base64Data}`;
25+
}

src/save/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export * from './encodePng.js';
33
export * from './encodeJpeg.js';
44
export * from './write.js';
55
export * from './writeCanvas.js';
6-
export * from './encodeBase64.js';
6+
export * from './encodeDataURL.js';

0 commit comments

Comments
 (0)