mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
950 B
40 lines
950 B
6 years ago
|
const gm = require('@tohru/gm')
|
||
|
, rr = (min, max) => Math.floor(Math.random() * (max-min + 1) + min)
|
||
|
, width = 200
|
||
|
, height = 80;
|
||
|
|
||
|
function getShape() {
|
||
|
const x1 = rr(width * 0.1, width * 0.9)
|
||
|
, y1 = rr(height * 0.1, height * 0.9)
|
||
|
, size = rr(15, 30)
|
||
|
, x2 = rr(x1, x1 + size)
|
||
|
, y2 = rr(y1, y1 + size);
|
||
|
return { x1, x2, y1, y2 };
|
||
|
}
|
||
|
|
||
5 years ago
|
module.exports = (text, captchaId) => {
|
||
6 years ago
|
return new Promise((resolve, reject) => {
|
||
5 years ago
|
text = text.split(''); //array of chars
|
||
6 years ago
|
const x = gm(200, 80, '#fff')
|
||
|
.fill('#000')
|
||
|
.fontSize(80)
|
||
|
for (let i = 0; i <6; i++) {
|
||
|
x.drawText(i*30, 55+rr(0,10), text[i])
|
||
|
}
|
||
|
for (let i = 0; i <4; i++) {
|
||
|
const shape = getShape();
|
||
|
x.strokeWidth(rr(3, 5))
|
||
|
x.drawCircle(shape.x1, shape.y1, shape.x2, shape.y1)
|
||
|
}
|
||
|
x.wave(10, rr(50,80))
|
||
|
.blur(1, 2)
|
||
|
.crop(200, 80, 0, 0)
|
||
5 years ago
|
.write(`./uploads/captcha/${captchaId}.png`, (err) => {
|
||
6 years ago
|
if (err) {
|
||
|
return reject();
|
||
|
}
|
||
|
return resolve();
|
||
|
});
|
||
|
})
|
||
|
}
|