-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscreenshot.js
More file actions
103 lines (84 loc) · 3.52 KB
/
screenshot.js
File metadata and controls
103 lines (84 loc) · 3.52 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const slugify = require('@sindresorhus/slugify');
const puppeteer = require('puppeteer');
var Jimp = require('jimp');
function makeIteratorThatFillsWithColor(color) {
return function (x, y, offset) {
this.bitmap.data.writeUInt32BE(color, offset, true);
}
};
var endpoint = "https://immortalsv.com";
if (process.env.DEV) {
endpoint = "http://localhost:3000";
}
async function getScreenshotForURL(url, shouldWatermark=true) {
const browser = await puppeteer.launch({
ignoreHTTPSErrors: true
});
const page = await browser.newPage();
page.setViewport({
width: 1024,
height: 768,
});
page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36");
page.goto(url);
await Promise.race([
page.waitForNavigation({waitUntil: 'load'}),
page.waitForNavigation({waitUntil: 'networkidle0'})
]);
await page.waitFor(1500);
const fileName = slugify(url + "-" + (new Date().getTime())) + ".jpg";
const uploadPath = "public/uploads/" + fileName;
const downloadPath = endpoint + "/uploads/" + fileName;
await page.screenshot({path: uploadPath, quality: 70});
await browser.close();
if (shouldWatermark) {
return Jimp.read(uploadPath)
.then(function (image) {
loadedImage = image;
return Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
})
.then(function (font) {
const paddingX = 10;
const text = "immortalsv.com " + url;
const width = Jimp.measureText(font, text);
const height = Jimp.measureTextHeight(font, text, width) * 0.95;
const overlay = new Jimp(width + (paddingX * 2), height, makeIteratorThatFillsWithColor(Jimp.cssColorToHex("#000000"))).quality(70);
overlay.scan(0, 0, width + (paddingX * 2), height, makeIteratorThatFillsWithColor(Jimp.cssColorToHex("#333")));
const watermark = overlay.print(font, paddingX, (height / 2) - 10, text)
const watermarkPath = "public/uploads/wm-" + fileName;
const watermarkDownloadPath = endpoint + "/uploads/wm-" + fileName;
loadedImage.quality(70).composite(watermark, 0, 768-height, {
mode: Jimp.BLEND_MULTIPLY,
opacitySource: 0.6,
opacityDest: 1,
}).quality(70).resize(768, 576).write(watermarkPath);
return new Promise(resolve => {
resolve({
"screenshot_url": watermarkDownloadPath,
"screenshot_path": watermarkPath,
});
});
})
.catch(function (err) {
console.log("Error while watermarking");
return new Promise(resolve => {
resolve(null);
});
});
} else {
const finalUploadPath = "public/uploads/uwm-" + fileName;
const finalDownloadPath = endpoint + "/uploads/uwm-" + fileName;
return Jimp.read(uploadPath).then(image => {
image.quality(70).resize(768, 576).write(finalUploadPath);
return new Promise(resolve => {
resolve({
"screenshot_url": finalDownloadPath,
"screenshot_path": finalUploadPath,
});
});
});
}
}
module.exports = {
'getScreenshotForURL': getScreenshotForURL
}