add distortion to captcha instead of wave using imagemagick and update dependency for gm fork

merge-requests/208/head
fatchan 5 years ago
parent d1292ec60f
commit 3abbe9b2c7
  1. 51
      helpers/captcha/captchagenerate.js
  2. 26
      package-lock.json
  3. 1
      package.json

@ -1,31 +1,36 @@
const gm = require('@tohru/gm')
const gm = require('gm').subClass({ imageMagick: true })
, 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 };
}
, height = 80
, distortion = 15
module.exports = (text, captchaId) => {
return new Promise((resolve, reject) => {
text = text.split(''); //array of chars
const x = gm(200, 80, '#fff')
const recy = rr(35,45);
const distorts = [];
const numDistorts = rr(3,5);
const div = width/numDistorts;
for (let i = 0; i < numDistorts; i++) {
const originx = rr((div*i)+distortion, (div*(i+1))-distortion);
const originy = rr(distortion,height-distortion);
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));
distorts.push([
{x:originx,y:originy}, //origin
{x:destx,y:desty} //dest
]);
}
const x = gm(width,height, '#fff')
.fill('#000')
.fontSize(70)
let lastx = 0;
.fontSize(65)
let lastx = 7;
for (let i = 0; i <6; i++) {
x.drawText(lastx, 45+rr(0,10), text[i])
x.drawText(lastx, 60, text[i])
switch (text[i]) {
case 'w':
case 'm':
lastx += 40;
break;
lastx += 45;
break;
case 'i':
case 'f':
case 'l':
@ -35,14 +40,12 @@ module.exports = (text, captchaId) => {
break;
default:
lastx += 30;
break;
break;
}
}
const recy1 = rr(20,40)
x.drawRectangle(rr(5,10), recy1, rr(190,195), recy1+5)
.wave(10, rr(80,120))
.blur(1, 2)
.crop(200, 80, 0, 0)
//.drawText(5, 60, text)
x.drawRectangle(5, recy, 195, recy+4)
.distort(distorts, 'Shepards')
.quality(30)
.write(`./static/captcha/${captchaId}.jpg`, (err) => {
if (err) {

26
package-lock.json generated

@ -2846,6 +2846,32 @@
"sparkles": "^1.0.0"
}
},
"gm": {
"version": "github:fatchan/gm#07df8fbf131b6b18c32fa010a84e67964e132955",
"from": "github:fatchan/gm",
"requires": {
"array-parallel": "^0.1.3",
"array-series": "^0.1.5",
"cross-spawn": "^4.0.0",
"debug": "^3.1.0",
"tmp": "^0.0.33"
},
"dependencies": {
"debug": {
"version": "3.2.6",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
"integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
"requires": {
"ms": "^2.1.1"
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
}
}
},
"graceful-fs": {
"version": "4.1.15",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz",

@ -16,6 +16,7 @@
"fluent-ffmpeg": "^2.1.2",
"fs": "0.0.1-security",
"fs-extra": "^7.0.1",
"gm": "github:fatchan/gm",
"mongodb": "^3.2.7",
"path": "^0.12.7",
"pug": "^2.0.3",

Loading…
Cancel
Save