-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add base64 image encoding #481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2e7515a
feat: add base64 string encoding
EscapedGibbon cc01564
test: add testing cases
EscapedGibbon 4831b6f
chore: fix prettier errors
EscapedGibbon a3ffd35
perf: change algorithm to improve performance
EscapedGibbon c623c5f
test: modify tests
EscapedGibbon 8a3792f
chore: formatting fixes
EscapedGibbon 5d3ba91
fix: get more reliable algo version
EscapedGibbon 73a299d
feat: add package for performant base64 encoding
EscapedGibbon de882ba
fix: remove vitest jsdom comments
EscapedGibbon fea99ac
fix: resolve conversations
EscapedGibbon 5bd614b
chore: update uint8-base64
EscapedGibbon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`basic image 2 (jpeg) 1`] = `"/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2MBERISGBUYLxoaL2NCOEJjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY//AABEIAAUABQMBEQACEQEDEQH/xAGiAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgsQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+gEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoLEQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqCg4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2dri4+Tl5ufo6ery8/T19vf4+fr/2gAMAwEAAhEDEQA/AOu0zTbmyvL+efUZbpLmTfHE+cQDLHaMk+oHbpQB/9k="`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { encode } from '../encode.js'; | ||
import { encodeDataURL } from '../encodeDataURL.js'; | ||
|
||
test('basic image (png)', () => { | ||
const image = testUtils.createGreyImage([ | ||
[0, 0, 0, 0, 0], | ||
[0, 255, 255, 255, 0], | ||
[0, 255, 0, 255, 0], | ||
[0, 255, 255, 255, 0], | ||
[255, 0, 255, 0, 255], | ||
]); | ||
const base64Url = encodeDataURL(image); | ||
|
||
expect(base64Url).toBe( | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAAAAACoBHk5AAAAFklEQVR4XmNggID///+DSCCEskHM/wCAnQr2TY5mOAAAAABJRU5ErkJggg==', | ||
); | ||
}); | ||
|
||
test('basic image 2 (jpeg)', () => { | ||
const image = testUtils.createGreyImage([ | ||
[255, 255, 255, 255, 255], | ||
[255, 0, 0, 0, 255], | ||
[255, 0, 0, 0, 255], | ||
[255, 0, 0, 0, 255], | ||
[255, 255, 255, 255, 255], | ||
]); | ||
const format = 'jpeg'; | ||
const base64 = encodeDataURL(image, { format }); | ||
const base64Data = Buffer.from(encode(image, { format })).toString('base64'); | ||
expect(typeof base64).toBe('string'); | ||
expect(base64Data).toMatchSnapshot(); | ||
}); | ||
|
||
test('legacy image-js test', () => { | ||
const image = testUtils.createRgbaImage( | ||
` | ||
255 0 0 / 255 | 0 255 0 / 255 | 0 0 255 / 255 | ||
255 255 0 / 255 | 255 0 255 / 255 | 0 255 255 / 255 | ||
0 0 0 / 255 | 255 255 255 / 255 | 127 127 127 / 255 | ||
`, | ||
); | ||
const format = 'jpeg'; | ||
const url = encodeDataURL(image, { format }); | ||
const base64Data = Buffer.from(encode(image, { format })).toString('base64'); | ||
expect(typeof url).toBe('string'); | ||
expect(base64Data).toBe(url.slice(url.indexOf(',') + 1)); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { encode as uint8encode } from 'uint8-base64'; | ||
|
||
import type { Image } from '../Image.js'; | ||
|
||
import type { | ||
EncodeOptionsBmp, | ||
EncodeOptionsJpeg, | ||
EncodeOptionsPng, | ||
} from './encode.js'; | ||
import { encode, defaultPng } from './encode.js'; | ||
/** | ||
* Converts image into Data URL string. | ||
* @param image - Image to get base64 encoding from. | ||
* @param options - Encoding options. | ||
* @returns base64 string. | ||
*/ | ||
export function encodeDataURL( | ||
image: Image, | ||
options: EncodeOptionsBmp | EncodeOptionsPng | EncodeOptionsJpeg = defaultPng, | ||
targos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
const buffer = encode(image, options); | ||
const base64 = uint8encode(buffer); | ||
const base64Data = new TextDecoder().decode(base64); | ||
return `data:image/${options.format};base64,${base64Data}`; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.