-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.js
61 lines (54 loc) · 1.64 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
60
61
const Canvas = require('canvas');
const fs = require('fs');
const Cache = require('./cache');
const WIDTH = 400;
const HEIGHT = 552;
const canvas = new Canvas(WIDTH, HEIGHT);
const ctx = canvas.getContext('2d');
const imageCacheOptions = {
bucket: process.env.S3_BUCKET,
generation: 'leave-img',
acl: 'public-read',
contentType: 'image/png',
s3: {
endpoint: process.env.S3_ENDPOINT,
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_SECRET_KEY,
},
};
const cache = new Cache(require('./cache/providers/s3'), imageCacheOptions);
const buildImageKey = numbers => `${numbers.join('')}.png`;
const drawToBuffer = numbers => {
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.font = "72px Impact";
ctx.fillStyle = "#000000";
ctx.fillText(numbers[0], 47, 180);
ctx.fillText(numbers[1], 135, 180);
ctx.fillText(numbers[2], 225, 180);
ctx.fillText(numbers[3], 310, 180);
return new Promise((resolve, reject) => {
fs.readFile(__dirname + '/foreground.png', function(err, squid) {
if (err) reject(err);
img = new Canvas.Image;
img.src = squid;
ctx.drawImage(img, 0, 0, img.width, img.height);
resolve(canvas.toBuffer());
});
})
}
const getCached = numbers =>
cache.getAndSetNotCached(
buildImageKey(numbers),
drawToBuffer.bind(this, numbers),
);
const getCachedMeta = numbers =>
cache.getMetaAndSetNotCached(
buildImageKey(numbers),
drawToBuffer.bind(this, numbers),
);
module.exports = {
drawToBuffer,
getCached,
getCachedMeta,
}