-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.js
82 lines (73 loc) · 2.06 KB
/
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
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
const path = require("path");
const jimp = require("jimp");
const { version } = require("./package.json");
const Vibrant = require("node-vibrant");
const { toPalette, toBase64 } = require("./util");
const ERROR_EXT = `Error: Input file is missing or of an unsupported image format lqip v${version}`;
// supported images aka mimetypes
const SUPPORTED_MIMES = {
jpeg: "image/jpeg",
jpg: "image/jpeg",
png: "image/png"
};
const base64 = file => {
return new Promise((resolve, reject) => {
// get the extension of the chosen file
let extension = path.extname(file) || "";
extension = extension.split(".").pop();
// supported files for now are ['jpg', 'jpeg', 'png']
if (!SUPPORTED_MIMES[extension]) {
return reject(ERROR_EXT);
}
return jimp
.read(file)
.then(image => image.resize(10, jimp.AUTO))
.then(image =>
image.getBuffer(SUPPORTED_MIMES[extension], (err, data) => {
if (err) {
return reject(err);
}
if (data) {
// valid image Base64 string, ready to go as src or CSS background
return resolve(toBase64(SUPPORTED_MIMES[extension], data));
}
return reject(
new Error('Unhandled promise rejection in base64 promise')
);
})
)
.catch(err => {
return reject(err);
});
});
};
const palette = file => {
return new Promise((resolve, reject) => {
// vibrant library was about 10-15% slower than
// get-image-colors npm module but provided better
// and more needed information
let vibrant = new Vibrant(file, {
// no special options for now
});
vibrant
.getPalette()
.then(palette => {
if (palette) {
return resolve(toPalette(palette));
}
return reject(
new Error("Unhandled promise rejection in colorPalette", palette)
);
})
.catch(err => {
return reject(err);
});
});
};
process.on("unhandledRejection", up => {
throw up;
});
module.exports = {
base64,
palette
};