项目作者: tormozz48

项目描述 :
Generate CAPTCHA from algebraic formula
高级语言: TypeScript
项目地址: git://github.com/tormozz48/algebraic-captcha.git
创建时间: 2018-12-21T22:15:40Z
项目社区:https://github.com/tormozz48/algebraic-captcha

开源协议:Apache License 2.0

下载


algebraic-captcha

Generate CAPTCHA from algebraic equation

version
npm license
Build Status
Commitizen friendly

DEMO

Install

Install package as yet another npm package:

  1. npm install algebraic-captcha

Also yarn package manager can be used:

  1. yarn add algebraic-captcha

Usage

Simple example.

  1. const {AlgebraicCaptcha} = require('algebraic-captcha');
  2. async function generateCaptcha() {
  3. const algebraicCaptcha = new AlgebraicCaptcha({
  4. width: 200,
  5. height: 200,
  6. background: '#ffffff',
  7. noise: 1,
  8. minValue: 1,
  9. maxValue: 10,
  10. operandAmount: 1,
  11. operandTypes: ['+', '-'],
  12. mode: 'formula',
  13. targetSymbol: '?'
  14. });
  15. const {image, answer} = await algebraicCaptcha.generateCaptcha();
  16. }

Here:

  • answer - is numeric answer for given equation (10 for this case).
  • image - svg image string
    image

Using inside express middleware.

  1. const express = require('express');
  2. const session = require('express-session');
  3. const {AlgebraicCaptcha} = require('algebraic-captcha');
  4. const app = express();
  5. const algebraicCaptcha = new AlgebraicCaptcha({});
  6. app.use(session({secret: 'my awesome session', resave: true}));
  7. app.get('/captcha', async (req, res) => {
  8. const {image, answer} = await algebraicCaptcha.generateCaptcha();
  9. req.session.captcha = answer;
  10. res.writeHead(200, {'Content-Type': 'image/svg+xml'});
  11. res.end(image);
  12. });
  13. app.listen(3000);

Options

Available options:

width.

Width of generated image in pixels

  • minimal value: 20
  • minimal value: 1000
  • default value: 200

height

Height of generated image in pixels

  • minimal value: 20
  • minimal value: 1000
  • default value: 100

minValue

Min operand value used in equation

  • minimal value: 1
  • minimal value: 10000
  • default value: 1

maxValue

Max operand value used in equation

  • minimal value: 1
  • minimal value: 10000
  • default value: 1

background

Background color of captcha image

  • default value: ‘#ffffff’

noise

Number of noise lines

  • default value: 1

operandAmount

Number of equation operations

  • default value: 1

operandTypes

Array of used math operators

  • default value: [‘+’, ‘-‘]

mode

Can have two available values: formula, equation

In formula mode anwer placeholder will be put to the last position of generated string, such as:

  1. 5 + 2 = ?

In equation mode answer placeholder will be put on random operand position except last:

  1. 5 + ? = 7 //(or ? + 2 = 7)
  • default value: ‘formula’

targetSymbol

Symbol which is used as placeholder for answer

  • default value: ‘?’

Development

Clone repository or its fork to local filesystem

  1. git clone https://github.com/tormozz48/algebraic-captcha.git

Install npm dependencies:

  1. npm install

Here are some suitable commands that can be used during development:

  • npm run build - compile typescript files
  • npm run lint - run tslint verification tool
  • npm test - run tests with mocha
  • npm run watch - launch watcher for compile source files during development

Important Use command npm run cm for commit your changes instead of git commit.

License

Apache-2.0

Support

Bugs, PRs, comments, suggestions welcomed!

Maintainer: andrey.kuznetsov48@yandex.ru">Andrey Kuznetsov