项目作者: yukrain

项目描述 :
适用于Koa框架的canvas绘制的验证码库
高级语言: JavaScript
项目地址: git://github.com/yukrain/koa-canvas-captcha.git
创建时间: 2016-05-09T08:36:49Z
项目社区:https://github.com/yukrain/koa-canvas-captcha

开源协议:MIT License

下载


Build Status
NPM version
Dependency Status

NPM

koa-canvas-captcha

适用于Koa框架的canvas绘制的验证码库

Install

  1. npm install koa-canvas-captcha --save

Demo

  1. var captcha = require('koa-canvas-captcha');
  2. var path = require("path");
  3. var getCaptcha = function*(){
  4. //生成验证码 返回text和图buffer
  5. var item = yield captcha({
  6. length: 4,
  7. fontSize: 30,
  8. width: 150,
  9. height: 32,
  10. color: 'green', // code color,
  11. background: 'rgb(245,245,245)', // captcha background color
  12. lineWidth: 0.5, // Interference lines width
  13. type: 'normal',
  14. fontPath: path.join(__dirname, './captchaFont.ttf')
  15. });
  16. //将验证码答案存在session
  17. this.session.captcha = item.answer;
  18. //返回图片
  19. this.type = 'jpg';
  20. this.set({
  21. 'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0',
  22. 'Expires': 'Sun, 12 Jan 1986 12:00:00 GMT'
  23. });
  24. this.body = item.imageBuffer
  25. };

Options

  • length - (Number ) captcha length
  • fontSize - (Number | default: 30 ) captcha font size
  • width - (Number | default: 150) image width
  • height - (Number | default: 32) height width
  • lineWidth - (Number | default: 1) background line width
  • background - (String | default: rgb(255,255,255)) background color
  • color - (String | default: rgb(0,0,0)) font color
  • text - (String) custom captcha text
  • fontPath - (String ) the *.ttf file path
  • type - (String | default: normal) captcha type
    • normal - random letter and number
    • letter - just letter
    • number - just number
    • arithmetic - random arithmetic

Result

  • text - the words on the captcha
  • answer - the answer should input
  • imageBuffer - imageBuffer