From 3abbe9b2c724c082713ee597781ec54a6542c715 Mon Sep 17 00:00:00 2001 From: fatchan Date: Sat, 29 Jun 2019 10:40:10 +0000 Subject: [PATCH] add distortion to captcha instead of wave using imagemagick and update dependency for gm fork --- helpers/captcha/captchagenerate.js | 51 ++++++++++++++++-------------- package-lock.json | 26 +++++++++++++++ package.json | 1 + 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/helpers/captcha/captchagenerate.js b/helpers/captcha/captchagenerate.js index 40f6f8dc..b6a29b04 100644 --- a/helpers/captcha/captchagenerate.js +++ b/helpers/captcha/captchagenerate.js @@ -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) { diff --git a/package-lock.json b/package-lock.json index b436e973..1fad032a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 9093f76c..d8b1315b 100644 --- a/package.json +++ b/package.json @@ -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",