-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·116 lines (110 loc) · 2.44 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
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
#!/usr/bin/env node
const Jimp = require("jimp");
const sharp = require("sharp");
const path = require("path");
const fontsMap = require("./fonts-map");
const ttf = require("./ttf");
const version = require(__dirname + "/package.json").version;
const argv = require("yargs")
.option("resize", {
alias: "r",
type: "number",
describe: "Resize picture to nxn pixels"
})
.option("bgColor", {
alias: "b",
type: "string",
default: "black",
describe: "CSS/Hex value of the background"
})
.option("color", {
alias: "c",
type: "string",
default: "white",
describe: "CSS/Hex value of the font"
})
.option("message", {
alias: "m",
type: "string",
default: "",
describe: "Message"
})
.option("fontsize", {
alias: "s",
type: "number",
default: 200,
describe: "Font size in pixels"
})
.option("fontfamily", {
alias: "f",
type: "string",
choices: ["anton", "comfortaa", "karla", "roboto", "montserrat"],
default: "montserrat",
describe: "Font-family"
})
// version
.alias('v', 'version')
.version(version)
.describe('v', 'show version information')
// help text
.alias('h', 'help')
.help('help')
.argv;
const imageSize = 2160;
const message = argv.message;
const width = 1800;
const fontSize = parseInt(argv.fontsize, 10);
const lines = Math.ceil((message.length * fontSize) / width);
const height = lines * 1.1 * fontSize;
const marginX = (imageSize - width) / 2;
const marginY = (imageSize - height) / 2;
const wob = async ({
message,
width,
height,
marginX,
marginY,
resize,
bgColor
}) => {
const image = await new Jimp(2160, 2160, bgColor);
const text = await ttf(
message,
width,
height,
path.join(__dirname, "fonts", fontsMap[argv.fontfamily]),
fontSize,
argv.color
);
image.composite(text, marginX, marginY);
const file = message.replace(/[^a-z0-9+]+/gi, "") + ".jpg";
await image.writeAsync(message.replace(/[^a-z0-9+]+/gi, "") + ".jpg");
if (resize) {
await sharp(file)
.resize(resize)
.toFile(file.replace(".jpg", resize.toString() + ".jpg"));
}
};
(async () => {
if (!argv.message) {
console.log("No messsage provided.");
process.exit();
}
console.log(`
Message: ${argv.message}
Font color: ${argv.color}
Font size: ${argv.fontsize}
Font family: ${argv.fontfamily}
Background color: ${argv.bgColor}
Resize: ${argv.resize || "none"}
`);
await wob({
message: argv.message,
width,
height,
marginX,
marginY,
resize: argv.resize && parseInt(argv.resize, 10),
bgColor: argv.bgColor
});
})();