-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathimage.js
More file actions
44 lines (40 loc) · 947 Bytes
/
image.js
File metadata and controls
44 lines (40 loc) · 947 Bytes
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
39
40
41
42
43
44
/*
PDFImage - embeds images in PDF documents
By Devon Govett
*/
import fs from 'fs';
import JPEG from './image/jpeg';
import PNG from './image/png';
class PDFImage {
static open(src, label) {
let data;
if (Buffer.isBuffer(src)) {
data = src;
} else if (src instanceof ArrayBuffer) {
data = Buffer.from(new Uint8Array(src));
} else {
const match = /^data:.+?;base64,(.*)$/.exec(src);
if (match) {
data = Buffer.from(match[1], 'base64');
} else {
data = fs.readFileSync(src);
if (!data) {
return;
}
}
}
if (data[0] === 0xff && data[1] === 0xd8) {
return new JPEG(data, label);
} else if (
data[0] === 0x89 &&
data[1] === 0x50 &&
data[2] === 0x4e &&
data[3] === 0x47
) {
return new PNG(data, label);
} else {
throw new Error('Unknown image format.');
}
}
}
export default PDFImage;