forked from ChrisCinelli/branded-qr-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createResized.js
48 lines (38 loc) · 1.19 KB
/
createResized.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
import Jimp from 'jimp';
import globby from 'globby';
import path from 'path';
import l from '../helpers/setupAndLogger';
l.enableAll();
// ////////
// Resize to 512 (including padding)
// ////////
const finalSize = 1024;
globby(path.resolve(__dirname, '../images/original/*.png')).then((files) => {
for (let i = 0; i < files.length; i++) {
const src = files[i];
const dst = path.resolve(__dirname, `../images/tmp/${path.basename(src, '.png')}-resized.png`);
l.info('Processing', src, ' -> ', dst);
Jimp.read(src).then((img) => {
let w;
let h;
let s;
if (img.bitmap.width < img.bitmap.height) {
w = Jimp.AUTO;
h = Math.floor(Math.min(finalSize, img.bitmap.height * 2) / 2);
s = h * 2;
} else {
w = Math.floor(Math.min(finalSize, img.bitmap.width * 2) / 2);
h = Jimp.AUTO;
s = w * 2;
}
img.resize(w, h);
const x = Math.floor((s - img.bitmap.width) / 2);
const y = Math.floor((s - img.bitmap.height) / 2);
new Jimp(s, s, (err, empty) => { // eslint-disable-line
empty.composite(img, x, y).write(dst);
});
}).catch((error) => {
l.error(error);
});
}
});