-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
83 lines (64 loc) · 2.19 KB
/
index.ts
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
import path from 'path';
import Jimp from 'jimp';
import { Font } from '@jimp/plugin-print';
const loadFont = Jimp.loadFont(Jimp.FONT_SANS_128_BLACK);
function getMimeType(extension: string) {
extension = extension.substr(1).toLowerCase();
switch (extension) {
case 'jpeg':
case 'jpg':
return Jimp.MIME_JPEG;
case 'png':
return Jimp.MIME_PNG;
}
throw new Error();
}
function fillImage(image: Jimp, color: number) {
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (x, y, offset) {
this.bitmap.data.writeUInt32BE(color, offset);
});
}
/**
* @param font
* @param {string} text
*/
function measureText(font: Font, text: string) {
const w = Jimp.measureText(font, text);
const h = Jimp.measureTextHeight(font, text, w + 10);
return [w, h];
}
export async function stubImage(image: Buffer, imagePath: string): Promise<Buffer> {
const extension = path.extname(path.basename(imagePath));
const mimeType = getMimeType(extension);
const jimpImage = await Jimp.read(image);
const w = jimpImage.bitmap.width;
const h = jimpImage.bitmap.height;
fillImage(jimpImage, 0xE5E5E5FF);
if (w >= 32 && h >= 16) {
const text = `${w}x${h}`;
const font = await loadFont;
const [textW, textH] = measureText(font, text);
const textImage = new Jimp(textW, textH) as unknown as Jimp;
textImage.print(font, 0, 0, text);
textImage.color([
{apply: 'red', params: [128]},
{apply: 'green', params: [128]},
{apply: 'blue', params: [128]}
]);
const scaleFactor = Math.min(1, (w * .8) / textW, (h * .8) / textH);
const textNewW = Math.round(textW * scaleFactor);
const textNewH = Math.round(textH * scaleFactor);
textImage.resize(textNewW, textNewH);
jimpImage.composite(
textImage,
Math.round(w / 2 - textNewW / 2),
Math.round(h / 2 - textNewH / 2)
);
}
return new Promise<Buffer>(resolve => jimpImage.getBuffer(mimeType, (error, buffer) => {
if (error) {
throw error;
}
resolve(buffer);
}));
}