let express = require(‘express‘); let captchapng = require(‘captchapng‘); let app = express(); app.get(‘/cap‘,function (req, res) { res.render(‘html‘,{a,b,c,d}) // width,height, numeric captcha 数字验证码 //他这个图片 每个数字之间是 80 这个图片的高度是30 let num = parseInt(Math.random()*9000+1000); // cookie/session req.session.recode = num; let p = new captchapng(80,30,num); 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) let img = p.getBase64(); //将 base64图片格式 转成了 buffer let imgbase64 = new Buffer(img,‘base64‘); res.set(‘Content-Type‘,‘image/png‘); res.send(imgbase64); }); app.listen(3000,function () { console.log(‘start‘) });
原文地址:https://www.cnblogs.com/l8l8/p/9123907.html
时间: 2024-10-10 13:16:43