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);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM