-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-resize.js
66 lines (54 loc) · 1.73 KB
/
test-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
'use strict';
const jimp = require('jimp');
const path = require('path');
async function main() {
const imgPath = process.argv[2];
const width = +process.argv[3];
const height = +process.argv[4];
const watermarkPath = process.argv[5];
const newImgPath = await resizeImage(imgPath, width, height);
await watermarkImageCopy(newImgPath, watermarkPath);
}
async function resizeImage(imgPath, width, height) {
// Checking of input parameters
if (!imgPath) {
console.error(
'!!! Path to the image must be setted as the first parameter.',
);
return;
}
if (Number.isNaN(width)) {
console.error(
'!!! Width for new image must be setted as the second parameter.',
);
return;
}
height = Number.isNaN(height) ? jimp.AUTO : height;
const newImgPath =
path.dirname(imgPath) + path.sep + 'resized_' + path.basename(imgPath);
const image = await jimp.read(imgPath);
await image.resize(width, height);
await image.quality(80);
await image.writeAsync(newImgPath);
console.log('Resizing was succesful');
return newImgPath;
}
async function watermarkImageCopy(destPath, watermarkPath) {
const newImgPath =
path.dirname(destPath) +
path.sep +
'watermarked_' +
path.basename(destPath);
const destImage = await jimp.read(destPath);
const watermarkImage = await jimp.read(watermarkPath);
// calc the center of destination image
const x = destImage.bitmap.width / 2 - watermarkImage.bitmap.width / 2;
const y = destImage.bitmap.height / 2 - watermarkImage.bitmap.height / 2;
await destImage.composite(watermarkImage, x, y, {
opacitySource: 0.5,
});
await destImage.writeAsync(newImgPath);
console.log('Watermarking was succesful');
return newImgPath;
}
main();