-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathimage.js
59 lines (44 loc) · 1.69 KB
/
image.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
const { join } = require('path');
const { createCanvas, Image, GlobalFonts } = require('@napi-rs/canvas');
const QRCode = require('qrcode');
const { readFile, writeFile } = require('fs/promises');
const canvas = createCanvas(600, 900);
const ctx = canvas.getContext('2d');
GlobalFonts.registerFromPath('./assets/Whitney.woff2', 'Whitney');
async function createImage(path, fingerprint) {
const banner = new Image();
banner.src = await readFile(join(__dirname, 'assets', 'banner.png')); // 1200x718
ctx.fillStyle = '#23272a';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(banner, 0, 0, 600, 359);
ctx.textAlign = 'center';
ctx.font = '52px Whitney';
ctx.fillStyle = '#ffffff';
ctx.fillText('Free Gift', 300, 420);
ctx.font = '28px Whitney';
ctx.fillStyle = '#96989d';
ctx.fillText('You\'ve been gifted a free nitro subscription!', 300, 470);
ctx.fillText('Scan this with the Discord mobile app to claim it.', 300, 505);
const qr = new Image();
qr.src = await qrPromise(fingerprint);
ctx.drawImage(qr, 150, 550);
const overlay = new Image();
overlay.src = await readFile(join(__dirname, 'assets', 'overlay.png')); // 100x100
ctx.drawImage(overlay, 250, 650);
writeFile(path, canvas.toBuffer());
}
function qrPromise(fingerprint) {
return new Promise((resolve, reject) => {
QRCode.toBuffer(
fingerprint,
{
width: 300,
margin: 2,
},
(err, buffer) => {
if (err) reject(err);
resolve(buffer);
})
})
}
module.exports = createImage;