-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestWithSharp.mjs
108 lines (91 loc) · 2.75 KB
/
TestWithSharp.mjs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import sharp from 'sharp';
import Debug from 'debug';
import icc from 'icc';
import exifReader from 'exif-reader';
const debug = Debug('App:TestWithSharp');
export async function GetExif(exifBuffer) {
return new Promise(resolve => {
resolve(exifReader(exifBuffer));
});
}
export default async function TestWithSharp({ imageStream, processExif = true, fileName }) {
debug(`Starting ${fileName}`);
const started = process.hrtime();
const resizeResult = await sharp(imageStream)
.resize(900, 900, {
withoutEnlargement: true,
fit: 'contain'
})
.withMetadata()
.toFormat('jpeg')
.toBuffer();
const image = sharp(resizeResult);
// try {
// image = sharp(imageStream);
// } catch (ex) {
// console.log(ex);
// return;
// }
// imageStream.pipe(image);
const metadata = await image.metadata();
let iccProfile;
let exifProfile;
console.log(metadata);
if (metadata.hasProfile && metadata.icc) {
try {
iccProfile = icc.parse(metadata.icc);
} catch (exception) {
console.log('ICC PROFILE PARSE ERROR:');
console.log(exception);
}
console.log('ICC PROFILE:');
console.log(iccProfile);
}
if (processExif && metadata.exif) {
try {
exifProfile = await GetExif(metadata.exif);
console.log('GOT EXIF!!!');
console.log(exifProfile);
} catch (exception) {
console.log('EXIF PARSE ERROR:');
console.log(exception);
}
}
let colorProfile = 'No Profile';
if (metadata.hasProfile && metadata.icc) {
if (iccProfile) {
if (iccProfile.description) {
colorProfile = iccProfile.description;
} else if (iccProfile.cmm) {
colorProfile = `${iccProfile.cmm} (Connection: ${iccProfile.connectionSpace})`;
} else {
colorProfile = 'Profile missing name';
}
} else {
colorProfile = 'Profile did not parse.';
}
}
let device = 'Unknown';
if (exifProfile && exifProfile.image) {
const deviceMake = exifProfile.image.Make;
const deviceModel = exifProfile.image.Model;
if (deviceMake && deviceModel) {
device = `${deviceMake} ${deviceModel}`;
}
}
debug(`Finished ${fileName}`);
return {
library: {
metadata,
iccProfile,
exifProfile
},
width: metadata.width,
height: metadata.height,
colorProfile,
colorSpaceName: metadata.space,
device,
libraryName: 'Sharp',
count: process.hrtime(started)
};
}