produck / svg-captcha

generate svg captcha in node
MIT License
1.86k stars 174 forks source link
captcha captcha-alternative captcha-generator nodejs-captcha svg-captcha

svg-captcha

[![Build Status](https://img.shields.io/travis/lemonce/svg-captcha/master.svg?style=flat-square)](https://travis-ci.org/lemonce/svg-captcha) [![NPM Version](https://img.shields.io/npm/v/svg-captcha.svg?style=flat-square)](https://www.npmjs.com/package/svg-captcha) [![NPM Downloads](https://img.shields.io/npm/dm/svg-captcha.svg?style=flat-square)](https://www.npmjs.com/package/svg-captcha)

generate svg captcha in node.js

Translations

中文

useful if you

install

npm install --save svg-captcha

usage

var svgCaptcha = require('svg-captcha');

var captcha = svgCaptcha.create();
console.log(captcha);
// {data: '<svg.../svg>', text: 'abcd'}

with express

var svgCaptcha = require('svg-captcha');

app.get('/captcha', function (req, res) {
    var captcha = svgCaptcha.create();
    req.session.captcha = captcha.text;

    res.type('svg');
    res.status(200).send(captcha.data);
});

API

svgCaptcha.create(options)

If no option is passed, you will get a random string of four characters and corresponding svg.

This function returns an object that has the following property:

svgCaptcha.createMathExpr(options)

Similar to create api, you have the above options plus 3 additional:

This function returns an object that has the following property:

svgCaptcha.loadFont(url)

Load your own font and override the default font.

svgCaptcha.options

Gain access to global setting object. It is used for create and createMathExpr api as the default options.

In addition to size, noise, color, and background, you can also set the following property:

svgCaptcha.randomText([size|options])

return a random string.

svgCaptcha(text, options)

return a svg captcha based on text provided.

In pre 1.1.0 version you have to call these two functions,
now you can call create() to save some key strokes ;).

sample image

default captcha image:

image

math expression image with color options:

image2

why use svg?

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

License

MIT