forked from flutterchina/flutter_in_action_2nd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgif.js
36 lines (31 loc) · 1.11 KB
/
gif.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
const qr = require('qr-image');
const {Image, createCanvas} = require('canvas')
const fs = require('fs')
let data = fs.readFileSync(__dirname + '/src/imgs/gif.txt')
console.log(data.toString().split("\n"))
let images = data.toString().split("\n");
images.forEach((e) => {
if (e.trim()) {
console.log(e);
createQRcode(e)
}
})
function createQRcode(str) {
let svg_string = qr.imageSync(`https://github.com/flutterchina/flutter_in_action_2nd/blob/main/src/imgs/${str}`, {type: 'png'});
const canvas = createCanvas(200, 240)
const ctx = canvas.getContext('2d')
ctx.fillStyle = '#fff'
ctx.fillRect(0, 0, canvas.width, canvas.height);
const img = new Image()
img.onload = () => ctx.drawImage(img, 0, 0, 200, 200)
img.src = svg_string;
ctx.font = '15px Impact'
ctx.fillStyle = '#000'
let _str = `图${str} (扫码查看动图)`;
let text = ctx.measureText(_str)
ctx.fillText(_str, (200 - text.width) / 2, 215)
const out = fs.createWriteStream(__dirname + `/src/gifs/${str}.jpeg`)
const stream = canvas.createJPEGStream()
stream.pipe(out)
console.log(__dirname + `/src/imgs/${str}.jpeg`);
}