-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreateImage.js
More file actions
85 lines (66 loc) · 2.65 KB
/
createImage.js
File metadata and controls
85 lines (66 loc) · 2.65 KB
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
import fetch from "node-fetch";
import Jimp from "jimp";
// Set 3 second delay (For compressions and resizes.)
function timeout(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function getQuote() {
console.log('Fetching a quote..');
let todaysData = await fetch("https://stoicquotesapi.com/v1/api/quotes/random")
.then((res) => res.json())
.then((json) => {
return json;
});
let todaysQuote = todaysData.body;
let todaysAuthor = todaysData.author;
return [todaysQuote, todaysAuthor];
}
async function splitString(string) {
console.log('Checking if the quote has more than 30 characters..');
let stringArray = string.split(" ");
let newArray = [];
let tempString = "";
for (let i = 0; i < stringArray.length; i++) {
if (tempString.length + stringArray[i].length + 1 > 30) {
console.log('\nWell it does..\n');
newArray.push(tempString);
tempString = "";
}
tempString += stringArray[i] + " ";
}
newArray.push(tempString);
return newArray;
}
async function buildImage() {
console.log('Now constructing image..');
let fetchedData = await getQuote();
let todaysQuote = fetchedData[0];
let rawQuote = fetchedData[0];
todaysQuote = await splitString(todaysQuote);
let todaysAuthor = fetchedData[1];
todaysAuthor = "- " + todaysAuthor;
const font = `assets/philosopher.fnt`;
Jimp.read('assets/postBackground.jpg', (err, image) => {
if (err) throw err;
Jimp.loadFont(`assets/philosopher.fnt`, (err, font) => {
var w = image.bitmap.width;
var h = image.bitmap.height;
var textWidth2 = Jimp.measureText(font, todaysAuthor);
var textHeight2 = Jimp.measureTextHeight(font, todaysAuthor);
for (let i = 0; i < todaysQuote.length; i++) {
var textHeight = Jimp.measureTextHeight(font, todaysQuote[i]);
var textWidth = Jimp.measureText(font, todaysQuote[i]);
image.print(font, w/2 - textWidth/2, h/6 + 100 - textHeight/2 + i*textHeight, todaysQuote[i]);
}
image.print(font, w/2 - textWidth2/2, textHeight2/2 + h/2 + 500,
{
text: todaysAuthor,
}, textWidth2, textHeight2)
.write('todaysPost.jpg'); // save
console.log(`An image quote has been constructed!`);
});
});
await timeout(1000);
return [rawQuote, todaysAuthor];
}
export { buildImage, getQuote };