/
app.js
103 lines (83 loc) · 2.93 KB
/
app.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
const express = require("express");
const path = require('path');
const bodyparser = require("body-parser");
const app = express();
const port = process.env.PORT || 3200;
const Jimp = require('jimp');
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
function jeromeSays(phrase){
//if you are following along, create the following 2 images relative to this script:
let rawImages = ['raw/jeromeSays.jpg','raw/musclesSays.jpg','raw/motivationSays.jpg','raw/captainJS.jpg']
let imgRaw = rawImages[Math.floor(Math.random() * rawImages.length)]; //a 1024px x 1024px backgroound image
let imgActive = 'active/heSaidIt.jpg';
let imgExported = 'export/heSaidIt.jpg';
let textData = {
text: phrase, //the text to be rendered on the image
maxWidth: 400, //image width - 10px margin left - 10px margin right
maxHeight: 267, //logo height + margin
placementX: 505, // 10px in on the x axis
placementY: 95 //bottom of the image: height - maxHeight - margin
};
//read template & clone raw image
Jimp.read(imgRaw)
.then(tpl => (tpl.clone().write(imgActive)))
//read cloned (active) image
.then(() => (Jimp.read(imgActive)))
//load font
.then(tpl => (
Jimp.loadFont(Jimp.FONT_SANS_32_BLACK).then(font => ([tpl, font]))
))
//add footer text
.then(data => {
tpl = data[0];
font = data[1];
return tpl.print(font, textData.placementX, textData.placementY, {
text: textData.text,
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
}, textData.maxWidth, textData.maxHeight);
})
//export image
.then(tpl => (tpl.quality(100).write(imgExported)))
//log exported filename
.then(tpl => {
console.log('exported file: ' + imgExported);
})
//catch errors
.catch(err => {
console.error(err);
});
}
app.use("/img", express.static(path.join(__dirname, 'img')));
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: false }));
app.get("/say/:phrase", (req, res) => {
const phrase = req.params.phrase
if(phrase !== 'favicon.ico'){
console.log(phrase)
jeromeSays(phrase)
sleep(2000).then(() => {
res.status(200).sendFile("./export/heSaidIt.jpg",{root: __dirname});
})
}
});
app.post("/say/", (req, res) => {
const phrase = req.body.text
if(phrase !== 'favicon.ico'){
let encodedPhrase = encodeURIComponent(phrase)
let newUrl = "http://jeromesays.gq/" + encodedPhrase
let result = {"text": newUrl}
return res.json(result)
// jeromeSays(phrase)
// sleep(2000).then(() => {
// res.status(200).sendFile("./export/heSaidIt.jpg",{root: __dirname});
}
});
app.get('/',function(req,res) {
res.sendFile("./creepyface.html",{root: __dirname});
});
app.listen(port, () => {
console.log(`running at port ${port}`);
});