generate svg captcha in node.js
- cannot or do not want to use google recaptcha
- have issue with install c++ addon
npm install --save svg-captcha
var svgCaptcha = require('svg-captcha');
// generate random text of length 4
var text = svgCaptcha.randomText();
// generate svg image
var captcha = svgCaptcha(text);
with express
var svgCaptcha = require('svg-captcha');
app.get('/captcha', function (req, res) {
var text = svgCaptcha.randomText();
var captcha = svgCaptcha(text);
req.session.captcha = text;
res.set('Content-Type', 'image/svg+xml');
res.status(200).send(captcha);
});
svgCaptcha.randomText([size|options])
By default, you will get a random string of four characters.
The optional params are:
- size: number
the size of the random string - options: object
you can specify the size of the random string and filter out some characters.
e.g. { size: 6, ignoreChars: '1234567890' }. This will generate a string of 6 characters with letters only.
It does not require any c++ addon.
The result image is smaller than jpeg image.
This has to be a joke. /<text.+>;.+</text>/g.test...
svg captcha uses opentype.js underneath, which means that there is no
'<text>1234</text>'.
You get
'<path fill="#444" d="M104.83 19.74L107.85 19.74L112 33.56L116.13 19.74L119.15 19.74L113.48 36.85...'
instead.
Even though you can write a program that convert svg to png, svg captcha has done its job
—— make captcha recognition harder