mirror of https://gitgud.io/fatchan/jschan.git
parent
c53d760350
commit
709e695ae7
3 changed files with 66 additions and 50 deletions
@ -1,61 +1,78 @@ |
|||||||
const gm = require('gm').subClass({ imageMagick: true }) |
const gm = require('gm').subClass({ imageMagick: true }) |
||||||
|
, { Captchas } = require(__dirname+'/../../db/') |
||||||
, uploadDirectory = require(__dirname+'/../files/uploadDirectory.js') |
, uploadDirectory = require(__dirname+'/../files/uploadDirectory.js') |
||||||
, rr = (min, max) => Math.floor(Math.random() * (max-min + 1) + min) |
, randomRange = (min, max) => Math.floor(Math.random() * (max-min + 1) + min) |
||||||
, width = 200 |
, characterWidth = (char) => { |
||||||
|
switch (char) { |
||||||
|
case 'w': |
||||||
|
case 'm': |
||||||
|
return 45; |
||||||
|
case 'i': |
||||||
|
case 'l': |
||||||
|
return 12; |
||||||
|
case 'f': |
||||||
|
case 'j': |
||||||
|
case 't': |
||||||
|
return 15; |
||||||
|
default: |
||||||
|
return 30; |
||||||
|
} |
||||||
|
} |
||||||
|
, totalWidth = (text) => { |
||||||
|
return text.split('').reduce((acc, char) => { |
||||||
|
return characterWidth(char) + acc + 1; |
||||||
|
}, 0); |
||||||
|
} |
||||||
|
, width = 210 |
||||||
, height = 80 |
, height = 80 |
||||||
, distortion = 15 |
, distortion = 13; |
||||||
|
|
||||||
module.exports = (text, captchaId) => { |
module.exports = async () => { |
||||||
return new Promise((resolve, reject) => { |
const text = Math.random().toString(36).substr(2,6); |
||||||
const recy = rr(35,45); |
const captchaId = await Captchas.insertOne(text).then(r => r.insertedId); |
||||||
const distorts = []; |
const distorts = []; |
||||||
const numDistorts = rr(3,5); |
const numDistorts = randomRange(3,5); |
||||||
const div = width/numDistorts; |
const div = width/numDistorts; |
||||||
|
|
||||||
|
|
||||||
for (let i = 0; i < numDistorts; i++) { |
for (let i = 0; i < numDistorts; i++) { |
||||||
const originx = rr((div*i)+distortion, (div*(i+1))-distortion); |
const divStart = (div*i) |
||||||
const originy = rr(distortion,height-distortion); |
, divEnd = (div*(i+1)); |
||||||
const destx = rr(Math.max(distortion,originx-distortion),Math.min(width-distortion,originx+distortion)); |
|
||||||
const desty = rr(Math.max(distortion,originy-distortion*2),Math.min(height-distortion,originy+distortion*2)); |
const originx = randomRange(divStart, divEnd) |
||||||
|
, originy = randomRange(0,height); |
||||||
|
|
||||||
|
const destx = randomRange(Math.max(distortion,originx-distortion),Math.min(width-distortion,originx+distortion)) |
||||||
|
, desty = randomRange(Math.max(distortion,originy-distortion*2),Math.min(height-distortion,originy+distortion*2)); |
||||||
|
|
||||||
distorts.push([ |
distorts.push([ |
||||||
{x:originx,y:originy}, //origin
|
{x:originx,y:originy}, //origin
|
||||||
{x:destx,y:desty} //dest
|
{x:destx,y:desty} //dest
|
||||||
]); |
]); |
||||||
|
|
||||||
} |
} |
||||||
const x = gm(width,height,'#ffffff') |
|
||||||
|
return new Promise((resolve, reject) => { |
||||||
|
const captcha = gm(width,height,'#ffffff') |
||||||
.fill('#000000') |
.fill('#000000') |
||||||
.fontSize(65) |
.fontSize(65); |
||||||
let lastx = 7; |
const startX = (width-totalWidth(text))/2; |
||||||
for (let i = 0; i <6; i++) { |
let charX = startX; |
||||||
x.drawText(lastx, 60, text[i]) |
for (let i = 0; i < 6; i++) { |
||||||
switch (text[i]) { |
captcha.drawText(charX, 60, text[i]); |
||||||
case 'w': |
charX += characterWidth(text[i]); |
||||||
case 'm': |
|
||||||
lastx += 45; |
|
||||||
break; |
|
||||||
case 'i': |
|
||||||
case 'l': |
|
||||||
lastx += 12; |
|
||||||
break; |
|
||||||
case 'f': |
|
||||||
case 'j': |
|
||||||
case 't': |
|
||||||
lastx += 15; |
|
||||||
break; |
|
||||||
default: |
|
||||||
lastx += 30; |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
} |
||||||
//.drawText(5, 60, text)
|
const lineY = randomRange(35,45); |
||||||
x.drawRectangle(5, recy, 195, recy+4) |
captcha |
||||||
|
.drawRectangle(startX, lineY, charX, lineY+4) |
||||||
.distort(distorts, 'Shepards') |
.distort(distorts, 'Shepards') |
||||||
.paint(2) |
.paint(2) |
||||||
// .quality(30)
|
|
||||||
.write(`${uploadDirectory}/captcha/${captchaId}.jpg`, (err) => { |
.write(`${uploadDirectory}/captcha/${captchaId}.jpg`, (err) => { |
||||||
if (err) { |
if (err) { |
||||||
return reject(err); |
return reject(err); |
||||||
} |
} |
||||||
return resolve(); |
return resolve({ id: captchaId, text }); |
||||||
}); |
}); |
||||||
}) |
}) |
||||||
} |
} |
||||||
|
|
||||||
|
@ -1,4 +1,4 @@ |
|||||||
noscript.no-m-p |
noscript.no-m-p |
||||||
iframe.captcha(src='/captcha.html' 'width=200' height='80' scrolling='no') |
iframe.captcha(src='/captcha.html' 'width=210' height='80' scrolling='no') |
||||||
.jsonly.captcha(style='display:none;') |
.jsonly.captcha(style='display:none;') |
||||||
input.captchafield(type='text' name='captcha' autocomplete='off' placeholder='captcha text' pattern=".{6}" required title='6 characters') |
input.captchafield(type='text' name='captcha' autocomplete='off' placeholder='captcha text' pattern=".{6}" required title='6 characters') |
||||||
|
Loading…
Reference in new issue