Authored by 陈轩

添加 captchapng

  1 +/**
  2 + * Created by TaoHuang on 2016/7/1.
  3 + */
  4 +
  5 +'use strict';
  6 +
  7 +const _ = require('lodash');
  8 +const Captchapng = require('captchapng');
  9 +
  10 +exports.generateCaptcha = (width, height, length) => {
  11 + let min = Math.pow(10, (length - 1 || 1));
  12 + let max = Math.pow(10, (length - 1 || 1)) * 9;
  13 + let token = '' + _.random(min, max);
  14 +
  15 + let png = new Captchapng(width, height, token);//
  16 +
  17 + png.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha)
  18 + png.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
  19 +
  20 + return {
  21 + image: new Buffer(png.getBase64(), 'base64'),
  22 + text: token
  23 + };
  24 +};
@@ -20,6 +20,7 @@ @@ -20,6 +20,7 @@
20 "dependencies": { 20 "dependencies": {
21 "bluebird": "^3.4.6", 21 "bluebird": "^3.4.6",
22 "body-parser": "^1.15.2", 22 "body-parser": "^1.15.2",
  23 + "captchapng": "0.0.1",
23 "cheerio": "^0.22.0", 24 "cheerio": "^0.22.0",
24 "compression": "^1.6.2", 25 "compression": "^1.6.2",
25 "connect-multiparty": "^2.0.0", 26 "connect-multiparty": "^2.0.0",