Node.js生成图形验证码--captchapng/ccap/trek-captcha

本文介绍了在Node.js中生成图形验证码的三种方式: captchapng(仅支持数字,适用于简单场景)、ccap(支持字母和数字,带干扰线,更安全,但不支持Windows)以及trek-captcha。通过示例展示了如何使用这些模块创建服务器端接口,以及在前端如何动态更换验证码图片,避免浏览器缓存。
摘要由CSDN通过智能技术生成

captchapng 是一个使用base64编码,只能生成数字验证码的node模块

使用npm安装captchapng

npm install --save captchapng

可以通过node.js的http模块搭建服务器,设置请求的接口地址,然后使用captchapng生成图形验证码返回给客户端;

var http = require('http');
var captchapng = require('captchapng');

http.createServer(function (request, response) {
   
    if(request.url === '/captcha') {
        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('');
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值