-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize.js
86 lines (77 loc) · 2.5 KB
/
resize.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
83
84
85
86
'use strict';
const config = require('config');
const jimp = require('jimp');
const path = require('path');
module.exports = async (imgName, callback) => {
try {
const srcFolderPath = config.has('srcFolderPath')
? config.get('srcFolderPath')
: undefined;
const destFolderPath = config.has('destFolderPath')
? config.get('destFolderPath')
: undefined;
const newWidth = config.has('newWidth')
? config.get('newWidth')
: undefined;
const newHeight = config.has('newHeight')
? config.get('newHeight')
: jimp.AUTO;
const watermarkImgPath = config.has('watermarkImgPath')
? config.get('watermarkImgPath')
: undefined;
const opacitySource = config.has('opacitySource')
? config.get('opacitySource')
: undefined;
// Checking of input parameters
let isChecked = true;
if (!imgName) {
console.error('!!! Image name must be setted as the first parameter.');
isChecked = false;
}
if (!srcFolderPath) {
console.error(
'!!! Destination folder for new image must be setted in config.',
);
isChecked = false;
}
if (!destFolderPath) {
console.error(
'!!! Destination folder for new image must be setted in config.',
);
isChecked = false;
}
if (!newWidth) {
console.error('!!! Width for new image must be setted in config.');
isChecked = false;
}
if (!watermarkImgPath) {
console.error('!!! Path to watermark image must be setted in config.');
isChecked = false;
}
if (!opacitySource) {
console.error('!!! Opacity must be setted in config.');
isChecked = false;
}
if (isChecked) {
const imgPath = srcFolderPath + path.sep + imgName;
const newImgPath =
destFolderPath + path.sep + 'resized_' + path.basename(imgPath);
const srcImage = await jimp.read(imgPath);
const watermarkImage = await jimp.read(watermarkImgPath);
await srcImage.resize(newWidth, newHeight);
// calc the center of source image
const x = srcImage.bitmap.width / 2 - watermarkImage.bitmap.width / 2;
const y = srcImage.bitmap.height / 2 - watermarkImage.bitmap.height / 2;
await srcImage.composite(watermarkImage, x, y, {
opacitySource: opacitySource,
});
await srcImage.quality(80);
await srcImage.writeAsync(newImgPath);
} else {
throw 'There were some errors during resizing.';
}
callback(null);
} catch (e) {
callback(e);
}
};