Signal-Desktop/ts/util/imagePathToBytes.ts

26 lines
629 B
TypeScript
Raw Normal View History

2021-07-19 19:26:06 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2021-09-24 00:49:05 +00:00
import { canvasToBytes } from './canvasToBytes';
2021-07-19 19:26:06 +00:00
2021-09-24 00:49:05 +00:00
export async function imagePathToBytes(src: string): Promise<Uint8Array> {
2021-07-19 19:26:06 +00:00
const image = new Image();
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
if (!context) {
throw new Error(
'imagePathToArrayBuffer: could not get canvas rendering context'
);
}
image.src = src;
await image.decode();
canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0);
2021-09-24 00:49:05 +00:00
return canvasToBytes(canvas);
2021-07-19 19:26:06 +00:00
}