-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
132 lines (108 loc) · 3.36 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const Jimp = require("jimp");
const fs = require("fs");
const path = require("path");
const configPath = path.join(process.cwd(), "settings.json");
let conf;
try {
const data = fs.readFileSync(configPath, "utf8");
conf = JSON.parse(data);
} catch (err) {
console.error("Error reading config file:", err);
process.exit(1);
}
function getCurrentDirectory() {
return process.cwd();
}
function combinePaths(...segments) {
return path.join(...segments);
}
function directoryExists(directoryPath) {
try {
fs.accessSync(directoryPath, fs.constants.F_OK);
return true;
} catch (err) {
return false;
}
}
function createDirectoryIfNotExists(directoryPath) {
if (!directoryExists(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
}
}
function fileExists(filePath) {
try {
fs.accessSync(filePath, fs.constants.F_OK);
return true;
} catch (err) {
return false;
}
}
function getFileNamesFromDirectory(directoryPath) {
try {
const fileNames = fs.readdirSync(directoryPath);
return fileNames;
} catch (err) {
console.error("Error reading directory:", err);
return [];
}
}
const inputPath = combinePaths(getCurrentDirectory(), conf.inputPath);
const outputPath = combinePaths(getCurrentDirectory(), conf.outputPath);
const logoFilePath = combinePaths(getCurrentDirectory(), conf.watermarkLogoFile);
if (!directoryExists(inputPath)) {
console.error("Input file directory not found: " + inputPath);
process.exit(1);
}
createDirectoryIfNotExists(outputPath);
if (!fileExists(logoFilePath)) {
console.error("Logo file not found: " + logoFilePath);
process.exit(1);
}
let inputImages = getFileNamesFromDirectory(inputPath);
console.log("Input images found: " + inputImages.length);
for (let img of inputImages) {
if (img.endsWith('.ini')) {
continue; // Skip .ini files
}
const inputFile = combinePaths(inputPath, img);
const outputFile = combinePaths(outputPath, img);
const resizeSize = parseInt(conf.images.resizeSize, 10);
const outputSize = parseInt(conf.images.outputSize, 10);
console.log("Working on " + img + " ..");
Jimp.read(inputFile, (err, inputImage) => {
if (err) throw err;
const outputImage = new Jimp(resizeSize, resizeSize, 0xffffffff);
outputImage.composite(
inputImage,
(resizeSize - inputImage.bitmap.width) / 2,
(resizeSize - inputImage.bitmap.height) / 2
);
outputImage.write(outputFile, (err) => {
if (err) throw err;
Jimp.read(outputFile, (err, resizedImage) => {
if (err) throw err;
resizedImage.resize(outputSize, outputSize);
resizedImage.write(outputFile, (err) => {
if (err) throw err;
const main = async (a) => {
const [image, logo] = await Promise.all([
Jimp.read(a),
Jimp.read(logoFilePath),
]);
logo.resize(logo.bitmap.width, Jimp.AUTO);
const X = image.bitmap.width - logo.bitmap.width - 10;
const Y = image.bitmap.height - logo.bitmap.height - 10;
return image.composite(logo, X, Y, [
{
mode: Jimp.BLEND_SCREEN,
opacitySource: conf.watermark.opacity,
opacityDest: 1,
},
]);
};
main(outputFile).then((image) => image.write(outputFile));
});
});
});
});
}