Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADDED] min/max and operator options to the exports.mathExpr, extended d tests and (english) readme #33

Merged
merged 2 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/node_modules

\.idea/
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.git
test
242 changes: 123 additions & 119 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,119 +1,123 @@
![svg-captcha](media/header.png)

<div align="center">

[![Build Status](https://img.shields.io/travis/lemonce/svg-captcha/master.svg?style=flat-square)](https://travis-ci.org/lemonce/svg-captcha)
[![NPM Version](https://img.shields.io/npm/v/svg-captcha.svg?style=flat-square)](https://www.npmjs.com/package/svg-captcha)
[![NPM Downloads](https://img.shields.io/npm/dm/svg-captcha.svg?style=flat-square)](https://www.npmjs.com/package/svg-captcha)

</div>

> generate svg captcha in node.js

## Translations
[中文](README_CN.md)

## useful if you

- cannot or do not want to use google recaptcha
- have issue with install c++ addon

## install
```
npm install --save svg-captcha
```

## usage
```Javascript
var svgCaptcha = require('svg-captcha');

var captcha = svgCaptcha.create();
console.log(captcha);
// {data: '<svg.../svg>', text: 'abcd'}
```
with express
```Javascript
var svgCaptcha = require('svg-captcha');

app.get('/captcha', function (req, res) {
var captcha = svgCaptcha.create();
req.session.captcha = captcha.text;

res.type('svg');
res.status(200).send(captcha.data);
});
```

## API

#### `svgCaptcha.create(options)`
If no option is passed, you will get a random string of four characters and corresponding svg.

* `size`: 4 // size of random string
* `ignoreChars`: '0o1i' // filter out some characters like 0o1i
* `noise`: 1 // number of noise lines
* `color`: true // characters will have distinct colors instead of grey, true if background option is set
* `background`: '#cc9966' // background color of the svg image

This function returns an object that has the following property:
* `data`: string // svg path data
* `text`: string // captcha text

#### `svgCaptcha.createMathExpr(options)`
Similar to create api, you have the same options and return value.
The difference is that data is a svg will be an math equation on screen
and text will be the result of that equation in string, otherwise the usage
is the same as above.

#### `svgCaptcha.loadFont(url)`
Load your own font and override the default font.
* `url`: string // path to your font
This api is a wrapper around loadFont api of opentype.js.
Your may need experiment around various options to make your own font accessible.
See the following api.

#### `svgCaptcha.options`
Gain access to global setting object.
It is used for create and createMathExpr api as the default options.

In addition to size, noise, color, and background, you can also set the following property:
* `width`: number // width of captcha
* `height`: number // height of captcha
* `fontSize`: number // captcha text size
* `charPreset`: string // random character preset

#### `svgCaptcha.randomText([size|options])`
return a random string.
#### `svgCaptcha(text, options)`
return a svg captcha based on text provided.

In pre 1.1.0 version you have to call these two functions,
now you can call create() to save some key strokes ;).

## sample image
default captcha image:

![image](media/example.png)

math expression image with color options:

![image2](media/example-2.png)

## why use svg?

It does not require any c++ addon.
The result image is smaller than jpeg image.

> This has to be a joke. /\<text.+\>;.+\<\/text\>/g.test...

svg captcha uses opentype.js underneath, which means that there is no
'&lt;text&gt;1234&lt;/text&gt;'.
You get
'&lt;path fill="#444" d="M104.83 19.74L107.85 19.74L112 33.56L116.13 19.74L119.15 19.74L113.48 36.85...'
instead.

Even though you can write a program that convert svg to png, svg captcha has done its job
—— make captcha recognition harder

## License
[MIT](LICENSE.md)
![svg-captcha](media/header.png)

<div align="center">

[![Build Status](https://img.shields.io/travis/lemonce/svg-captcha/master.svg?style=flat-square)](https://travis-ci.org/lemonce/svg-captcha)
[![NPM Version](https://img.shields.io/npm/v/svg-captcha.svg?style=flat-square)](https://www.npmjs.com/package/svg-captcha)
[![NPM Downloads](https://img.shields.io/npm/dm/svg-captcha.svg?style=flat-square)](https://www.npmjs.com/package/svg-captcha)

</div>

> generate svg captcha in node.js

## Translations
[中文](README_CN.md)

## useful if you

- cannot or do not want to use google recaptcha
- have issue with install c++ addon

## install
```
npm install --save svg-captcha
```

## usage
```Javascript
var svgCaptcha = require('svg-captcha');

var captcha = svgCaptcha.create();
console.log(captcha);
// {data: '<svg.../svg>', text: 'abcd'}
```
with express
```Javascript
var svgCaptcha = require('svg-captcha');

app.get('/captcha', function (req, res) {
var captcha = svgCaptcha.create();
req.session.captcha = captcha.text;

res.type('svg');
res.status(200).send(captcha.data);
});
```

## API

#### `svgCaptcha.create(options)`
If no option is passed, you will get a random string of four characters and corresponding svg.

* `size`: 4 // size of random string
* `ignoreChars`: '0o1i' // filter out some characters like 0o1i
* `noise`: 1 // number of noise lines
* `color`: true // characters will have distinct colors instead of grey, true if background option is set
* `background`: '#cc9966' // background color of the svg image

This function returns an object that has the following property:
* `data`: string // svg path data
* `text`: string // captcha text

#### `svgCaptcha.createMathExpr(options)`
Similar to create api, you have the above options plus 3 additional:
* `mathMin`: 1 // the minimum value the math expression can be
* `mathMax`: 9 // the maximum value the math expression can be
* `mathOperator`: + // The operator to use, `+`, `-` or `+-` (for random `+` or `-`)

This function returns an object that has the following property:
* `data`: string // svg of the math expression
* `text`: string // the answer of the math expression

#### `svgCaptcha.loadFont(url)`
Load your own font and override the default font.
* `url`: string // path to your font
This api is a wrapper around loadFont api of opentype.js.
Your may need experiment around various options to make your own font accessible.
See the following api.

#### `svgCaptcha.options`
Gain access to global setting object.
It is used for create and createMathExpr api as the default options.

In addition to size, noise, color, and background, you can also set the following property:
* `width`: number // width of captcha
* `height`: number // height of captcha
* `fontSize`: number // captcha text size
* `charPreset`: string // random character preset

#### `svgCaptcha.randomText([size|options])`
return a random string.
#### `svgCaptcha(text, options)`
return a svg captcha based on text provided.

In pre 1.1.0 version you have to call these two functions,
now you can call create() to save some key strokes ;).

## sample image
default captcha image:

![image](media/example.png)

math expression image with color options:

![image2](media/example-2.png)

## why use svg?

It does not require any c++ addon.
The result image is smaller than jpeg image.

> This has to be a joke. /\<text.+\>;.+\<\/text\>/g.test...

svg captcha uses opentype.js underneath, which means that there is no
'&lt;text&gt;1234&lt;/text&gt;'.
You get
'&lt;path fill="#444" d="M104.83 19.74L107.85 19.74L112 33.56L116.13 19.74L119.15 19.74L113.48 36.85...'
instead.

Even though you can write a program that convert svg to png, svg captcha has done its job
—— make captcha recognition harder

## License
[MIT](LICENSE.md)
16 changes: 16 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ declare class ConfigObject {
* background color of svg image
*/
background?: string;
/**
* default: +
* the math operator to use, "+", "-" or "+/-"
* if unknown operator passed defaults to "+/-"
*/
mathOperator?: string;
/**
* default: 1
* min value of the math expression
*/
mathMin?: number;
/**
* default: 9
* max value of the math expression
*/
mathMax?: number;
}
/**
* result of captcha generation
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const create = function (options) {
};

const createMathExpr = function (options) {
const expr = random.mathExpr();
const expr = random.mathExpr(options.mathMin, options.mathMax, options.mathOperator);
const text = expr.text;
const data = createCaptcha(expr.equation, options);

Expand Down
40 changes: 31 additions & 9 deletions lib/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,39 @@ exports.captchaText = function (options) {
return out;
};

const mathExprPlus = function(leftNumber, rightNumber){
const text = (leftNumber + rightNumber).toString();
const equation = leftNumber + '+' + rightNumber;
return {text, equation}
}

const mathExprMinus = function(leftNumber, rightNumber){
const text = (leftNumber - rightNumber).toString();
const equation = leftNumber + '-' + rightNumber;
return {text, equation}
}

/**
* Returns an object that has the following props:
* text, equation
* Creates a simple math expression using either the + or - operator
* @param {number} [min] - The min value of the math expression defaults to 1
* @param {number} [max] - The max value of the math expression defaults to 9
* @param {string} [operator] - The operator(s) to use
* @returns {{equation: string, text: string}}
*/
exports.mathExpr = function () {
const left = randomInt(1, 9);
const right = randomInt(1, 9);
const text = (left + right).toString();
const equation = left + '+' + right;

return {text, equation};
exports.mathExpr = function (min, max, operator) {
min = min || 1;
max = max || 9;
operator = operator || '+';
const left = randomInt(min, max);
const right = randomInt(min, max);
switch(operator){
case '+':
return mathExprPlus(left, right)
case '-':
return mathExprMinus(left, right)
default:
return (randomInt(1, 2) % 2) ? mathExprPlus(left, right) : mathExprMinus(left, right);
}
};

// https://github.com/jquery/jquery-color/blob/master/jquery.color.js#L432
Expand Down
Loading