有一些Node.JS图片生成类库,比如node-captcha等的类库,需要c/c++程序生成图片。跨平台部署不是很方便。这里介绍几个用纯JS实现的图片验证码生成模块。
captchapng
用纯JavaScript实现的验证码生成模块。
https://github.com/GeorgeChan/captchapng
安装简单,依赖少:
npm install captchapng
示例:
var captchapng = require('captchapng'); app.get('/sign/captcha.png', function(req, res) { var captchaNumber = parseInt(Math.random() * 9000 + 1000) req.session.captcha = captchaNumber var p = new captchapng(80,20, captchaNumber); // 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'); res.writeHead(200, { 'Content-Type': 'image/png' }); res.end(imgbase64); })
Express + Captcha
为Express框架设计的验证码生成模块。
https://github.com/napa3um/node-captcha
安装&示例:
$ npm install captcha Usage (for Express 4) 'use strict' const express = require('express') const session = require('express-session') const bodyParser = require('body-parser') const captchaUrl = '/captcha.jpg' const captchaId = 'captcha' const captchaFieldName = 'captcha' const captcha = require('./captcha').create({ cookie: captchaId }) const app = express() app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: true, })) app.use(bodyParser.urlencoded({ extended: false })) app.get(captchaUrl, captcha.image()) app.get('/', (req, res) => { res.type('html') res.end(` <img src="/UploadFiles/2021-04-02/${ captchaUrl }">前端滑块验证
前端生成轨迹发送到后端验证,输入简单,但是容易被破解。
https://gitee.com/LongbowEnterprise/SliderCaptcha
总结
以上所述是小编给大家介绍的Node.JS用纯JavaScript生成图片或滑块式验证码功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
最新资源
- 【中国民乐】张维良《秋江夜泊》2004[FLAC+CUE]
- 明达年度发烧碟MasterSuperiorAudiophile2011[D
- 明达年度发烧碟MasterSuperiorAudiophile2017[D
- 翁立友.2008-恨孤单【大旗】【WAV+CUE】
- 群星.2009-Magic.Cover日粤精华3CD【环球】【WA
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][5
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量