-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
185 lines (162 loc) · 4.12 KB
/
index.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const fs = require("fs");
const puppeteer = require("puppeteer");
const OSS = require('ali-oss');
const client = new OSS({
region: ' your region, oss-cn-beijing',
accessKeyId: 'your accessKeyId',
accessKeySecret: 'your accessKeySecret',
bucket: 'your bucket name',
});
async function captureScreenshot(htmlContent) {
const browser = await puppeteer.launch({
headless: true,
args: [
"--disable-gpu",
"--disable-dev-shm-usage",
"--disable-setuid-sandbox",
"--no-first-run",
"--no-zygote",
"--no-sandbox",
],
});
const page = await browser.newPage();
// 设置视口大小
await page.setViewport({ width: 820, height: 964 });
// 设置页面内容
await page.setContent(htmlContent);
let path = "/tmp/example";
await page.screenshot({ path: path, fullPage: true, type: "png" });
await browser.close();
try {
const currentDate = new Date().getTime();
// 生成文件名
const fileName = `${currentDate}.png`;
console.log(fileName); // 输出格式为 YYYYMMDD,例如 20240717
const fileData = fs.createReadStream(path);
let result = await client.putStream(fileName, fileData);
console.log(result);
return {
body: result,
headers: {
"content-type": "application/json",
},
statusCode: 200,
}
} catch (e) {
console.log(e)
return {
body: e.message,
headers: {
"content-type": "application/json",
},
statusCode: 500,
}
}
}
exports.handler = async function (event, context, callback) {
var eventObj = JSON.parse(event.toString());
var title = '';
var content = '';
var footer = '';
// get http request body
if ("body" in eventObj) {
body = JSON.parse(eventObj.body);
if (eventObj.isBase64Encoded) {
body = Buffer.from(body, 'base64').toString('utf-8');
}
title = body['title'];
content = body['content'];
footer = body['footer'];
} else {
console.log("else:---->",event);
title = eventObj['title'];
content = eventObj['content'];
footer = eventObj['footer'];
}
const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Text to Image</title>
<link rel="stylesheet" href="//unpkg.com/heti/umd/heti.min.css">
<style>
.image-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
max-width: 800px;
margin: 0 auto;
}
.image-container img {
width: 100%;
height: auto;
display: block;
}
.text-overlay {
position: absolute;
color: rgb(0, 0, 0);
font-size: 2vw;
font-family: Arial, sans-serif;
padding: 1vw;
text-align: center;
transform: rotate(6deg);
max-width: 60%;
top: 25%;
}
.text-overlay #title{
font-size: 3vw;
text-align: center;
}
.text-overlay #footer{
text-align: right;
}
@media (max-width: 600px) {
.text-overlay {
font-size: 4vw;
padding: 2vw;
}
}
</style>
</head>
<body>
<div class="image-container">
<img src="https://telegraph-image.pages.dev/file/36594230fcca22c0480b9.jpg" alt="Image">
<div class="text-overlay">
<article class="entry heti heti--poetry">
<p id="title">
${title}
</p>
<p id="content">
${content}
</p>
<p id="footer">
${footer}
</p>
</article>
</div>
</div>
</body>
<script src="//unpkg.com/heti/umd/heti-addon.min.js"></script>
<script>
const heti = new Heti('.heti');
heti.autoSpacing();
</script>
</html>
`;
try {
const ret = await captureScreenshot(htmlContent);
callback(null, ret);
} catch (err) {
callback(null, {
body: err.message,
headers: {
"content-type": "application/json",
},
statusCode: 500,
});
}
};