-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
35 lines (29 loc) · 836 Bytes
/
index.js
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
import png from './types/png.js';
import jpeg from './types/jpeg.js';
import gif from './types/gif.js';
import webp from './types/webp.js';
import avif from './types/avif.js';
export function imageDimensionsFromData(bytes) {
// The shortest signature is 3 bytes.
if (bytes.length < 3) {
return;
}
// Prevent issues with Buffer being passed. Seems to be an issue on Node.js 20 and later.
bytes = new Uint8Array(bytes);
// Note: Place types that can be detected fast first.
return png(bytes)
?? gif(bytes)
?? jpeg(bytes)
?? webp(bytes)
?? avif(bytes);
}
export async function imageDimensionsFromStream(stream) {
const chunks = [];
for await (const chunk of stream) {
chunks.push(...chunk);
const dimensions = imageDimensionsFromData(new Uint8Array(chunks));
if (dimensions) {
return dimensions;
}
}
}