UNPKG

captcha-generator

Version:

Production image verification code by jpeglib

121 lines (94 loc) 4.11 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; exports.generate = generate; exports.createPlaintext = createPlaintext; var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _hcaptha = require('../build/Release/hcaptha.node'); var _hcaptha2 = _interopRequireDefault(_hcaptha); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var tmpPath = _path2.default.join(__dirname, "/tmp/"); !_fs2.default.existsSync(tmpPath) && _fs2.default.mkdirSync(tmpPath); var defaultDictionary = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", /*"l",*/"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", /*"I",*/"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]; var platform = process.platform; function generate() { var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : createPlaintext(); var width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 240; var height = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 112; var charOffset = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; var fontSize = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 100; if ((typeof text === 'undefined' ? 'undefined' : _typeof(text)) === "object") { var _text = text, t = _text.text, w = _text.width, h = _text.height, c = _text.charOffset, f = _text.fontSize; text = t; width = w; height = h; charOffset = c; fontSize = f; } var isJpeg = ~["linux"].indexOf(platform) ? 1 : 0; var tmpFilePath = _path2.default.join(tmpPath, '/' + Math.random() + '.' + (isJpeg ? "jpeg" : "bmp")); var defaultQuality = 60; charOffset = charOffset || width / text.length; _hcaptha2.default.create(text, tmpFilePath, text.length, width, height, charOffset, defaultQuality, isJpeg, fontSize); var buf = _fs2.default.readFileSync(tmpFilePath); _fs2.default.unlinkSync(tmpFilePath); return CImg(buf, text, isJpeg ? "jpeg" : "bmp"); } function createPlaintext() { var count = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 5; var dictionary = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultDictionary; if (isNaN(count) || !(dictionary instanceof Array) || dictionary.length === 0) { throw Error("Parameter error"); } var i = 0, len = dictionary.length, textList = ""; while (i++ < count) { textList += dictionary[parseInt(Math.random() * len) % len]; } return textList; } function CImg(buf, text, type) { var obj = {}; return Object.defineProperties(obj, { buffer: { get: function get() { return buf; }, enumerable: true, configurable: false }, plaintext: { get: function get() { return text; }, enumerable: true, configurable: false }, imageType: { get: function get() { return 'image/' + type; }, enumerable: true, configurable: false }, base64: { get: function get() { return 'data:image/' + type + ';base64,' + buf.toString("base64"); }, enumerable: true, configurable: false } }); } //hrobj.create("abcde", __dirname + "/c/" + start + ".bmp", 5, 240, 112, 50, 50, 0, 110);