captchapng是一个基于pnglib模块开发,数字型验证码模块。内置字体、全JavaScript无其它依赖。不像有的验证码需要依赖canvas或者是需要编译,而且captchapng使用起来简单。
安装模块:
npm install captchapng --save
实例app.js:
var http = require('http'); var captchapng = require('captchapng'); http.createServer(function (request, response) { if(request.url == '/captcha.png') { var p = new captchapng(80,30,parseInt(Math.random()*9000+1000)); // width,height,numeric captcha p.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha) p.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha) var img = p.getBase64(); var imgbase64 = new Buffer(img,'base64'); response.writeHead(200, { 'Content-Type': 'image/png' }); response.end(imgbase64); } else response.end(''); }).listen(8181); console.log('Web server started.\n http:\\\\127.0.0.1:8181\\captcha.png');
前段代码如下index.html:
<html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <img src="http:\\127.0.0.1:8181\captcha.png"> </body> </html>
打开页面你会看到如下结果“
虽然简单。。但是基本的验证还是可以的。。。
在cnode上面看到一篇文章如下:https://cnodejs.org/topic/581b2502e90cfbec054d763f
顺便自己试了下,感觉还可以:包地址如下:https://github.com/zengming00/node-gd-bmp
结果如下: