首页 > 代码库 > node.js利用captchapng模块实现图片验证码

node.js利用captchapng模块实现图片验证码

 

安装captchapng模块

npm install captchapng

 

nodejs中使用

var express = require(‘express‘);
var captchapng = require(‘captchapng‘);

//验证码
exports.verify= function(req, res, next) {
    var code = parseInt(Math.random() * 9000 + 1000);
    req.session.checkcode = code;
    var p = new captchapng(100, 30, code);
    p.color(0, 0, 0, 0);
    p.color(80, 80, 80, 255);
    var img = p.getBase64();
    var imgbase64 = new Buffer(img, ‘base64‘);
    res.writeHead(200, {
        ‘Content-Type‘: ‘image/png‘
    });
    res.end(imgbase64);
}

 

node.js利用captchapng模块实现图片验证码