node验证码生成工具

推荐一款node验证码生成工具 ccap

以下内容翻译自官方说明

不需要安装额外的库和软件,只需要用npm安装ccap就可以生成验证码。
支持的node版本 0.12.x and 4.x.x

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var captcha = ccap();

var captcha = ccap(width, height, offset);

var captcha = ccap({

width:256,//set width,default is 256

height:60,//set height,default is 60

offset:40,//set text spacing,default is 40

quality:100,//set pic quality,default is 50

fontsize:57,//set font size,default is 57

generate:function(){//Custom the function to generate captcha text

//generate captcha text here

return text;//return the captcha text

}

});

简单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var http = require('http');

var ccap = require('ccap')();//Instantiated ccap class

http.createServer(function (request, response) {

if(request.url == '/favicon.ico')return response.end('');//Intercept request favicon.ico

var ary = ccap.get();

var txt = ary[0];

var buf = ary[1];

response.end(buf);

console.log(txt);

}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

API

1
2
3
4
5
6
7
var captcha = ccap();

var ary = captcha.get();//ary[0] is captcha's text,ary[1] is captcha picture buffer.

var text = ary[0];

var buffer = ary[1];

停止或开启缓存定时器

1
2
3
4
5
6
7
8
9
10
11
var ccap = require('ccap')();

ccap.timerIsRunning(); //now timer is running, return 1

ccap.clearTimeout(); //stop all create capature image, so will use the same the 20(default is 20) capature images

ccap.timerIsRunning();//now timer is stoped, return 0

captcha8.setTimeout();//now timer is start again

ccap.timerIsRunning();//now timer is stoped, return 1