-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
174 lines (168 loc) · 3.75 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
const sharp = require('sharp');
const { rgb } = require('color');
const generator = require('generate-password');
const fs = require('fs');
const random = (min, max) => {
return Math.floor(Math.random() * (min + max - 1) + 1);
};
const pCreator = (number) => {
let str = '';
for (let i = 0; i < number; i++) {
str += ` <path d="M${random(1, 50)} ${random(0, 50)}C${random(
10,
150
)} ${random(10, 90)},${random(150, 90)} ${random(10, 99)},${random(
150,
190
)} 9
" class="p1" stroke="rgb(${random(0, 256)},${random(0, 256)},${random(
0,
256
)})" ></path>`;
}
return str;
};
const lCreator = (number) => {
let str = '';
for (let i = 0; i < number; i++) {
str += `<line stroke-width="${random(1, 5)}" y1="${random(
10,
30
)}" y2="${random(20, 80)}" x1="${random(10, 100)}" x2="${random(
30,
200
)}" class="l1" stroke="rgb(${random(0, 256)},${random(0, 256)},${random(
0,
256
)})"></line>`;
}
return str;
};
const tCreator = (text) => {
let str = '';
let x = 45;
for (let i = 0; i < text.length; i++) {
str += `<text id="text${i + 1}" x="${x}"
transform="rotate(${random(0, 12)} ${random(0, 15)} ${random(0, 20)})"
y="55">${text[i]}</text>`;
x = x + 20;
}
return str;
};
module.exports = async ({
numbers,
lowercase,
uppercase,
symbols,
difficulty,
path,
}) => {
return new Promise(async (res, rej) => {
try {
fs.mkdir(path, (err) => {
if (err.code != 'EEXIST') {
console.log(err);
}
});
const rgbs = rgb(random(0, 255), random(0, 255), random(0, 255));
const text = generator.generate({
length: 6,
numbers,
lowercase,
uppercase,
symbols,
});
const file = `${generator.generate({
length: 16,
numbers: false,
lowercase: true,
uppercase: true,
symbols: false,
})}.png`;
const width = 200;
const height = 100;
const svgImage = `
<svg width="200" height="100" viewBox="10 10 0 0">
<style>
#text1 {
font-size: 40px;
fill : ${rgbs};
stroke:rgb(${random(0, 255)},${random(0, 255)},${random(0, 255)});
stroke-width:${random(1, 3)}
}
#text2 {
font-size: 40px;
fill : ${rgbs};
stroke:rgb(${random(0, 255)},${random(0, 255)},${random(0, 255)});
stroke-width:${random(1, 3)}
}
#text3 {
font-size: 40px;
fill : ${rgbs};
stroke:rgb(${random(0, 255)},${random(0, 255)},${random(0, 255)});
stroke-width:${random(1, 3)}
}
#text4 {
font-size: 40px;
fill : ${rgbs};
stroke:rgb(${random(0, 255)},${random(0, 255)},${random(0, 255)});
stroke-width:${random(1, 3)}
}length
length
length
#text5 {
font-size: 40px;
fill : ${rgbs};
stroke:rgb(${random(0, 255)},${random(0, 255)},${random(0, 255)});
stroke-width:${random(1, 3)}
}
#text5 {
font-size: 40px;
fill : ${rgbs};
stroke:rgb(${random(0, 255)},${random(0, 255)},${random(0, 255)});
stroke-width:${random(1, 3)}
}
#text6 {
font-size: 40px;
fill : ${rgbs};
stroke:rgb(${random(0, 255)},${random(0, 255)},${random(0, 255)});
stroke-width:${random(1, 3)}
}
.p1 {
stroke-width: 5px;
fill:none;
}
</style>
${pCreator(difficulty / 5 < 1 ? 2 : difficulty / 5)}
${lCreator(difficulty * 5)}
${tCreator(text)}
</svg>
`;
const svgBuffer = Buffer.from(svgImage);
const image = await sharp({
create: {
width,
height,
channels: 4,
background: rgbs,
},
})
.composite([
{
input: svgBuffer,
top: 0,
left: 0,
},
])
.toFile(path + '/' + file)
.then(() => {
res({
file_name: file,
captcha: text,
});
});
} catch (error) {
rej(error);
}
});
};