-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheif.ts
38 lines (31 loc) · 876 Bytes
/
heif.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { createWorkerFactory } from '@shopify/web-worker';
import {
blobToFile,
bufferToBlob,
getExtensionFromMimeType,
getFileBasename,
} from '@mexp/media-utils';
const createHeifWorker = createWorkerFactory(
() => import( /* webpackChunkName: 'heif' */ '@mexp/heif' )
);
const heifWorker = createHeifWorker();
export async function transcodeHeifImage(
file: File,
type: 'image/jpeg' | 'image/png' | 'image/webp' = 'image/jpeg',
quality = 0.82
) {
const { buffer, width, height } = await heifWorker.transcodeHeifImage(
await file.arrayBuffer()
);
const blob = await bufferToBlob( buffer, width, height, type, quality );
return blobToFile(
blob,
`${ getFileBasename( file.name ) }.${ getExtensionFromMimeType(
type
) }`,
type
);
}
export async function isHeifImage( fileBuffer: ArrayBuffer ) {
return heifWorker.isHeifImage( fileBuffer );
}