Generate single-path svg captcha in node.js or express.js
Go to file
2022-06-18 11:33:57 +02:00
.github [UPDATED] code-of-conduct 2017-08-16 17:06:13 +08:00
.vscode [UPDATED] jest vs code debug file 2017-08-09 16:11:20 +08:00
fonts [ADDED] font LICENSE 2016-08-10 09:31:20 +08:00
lib Fix createCaptcha() not working. 2021-11-29 18:36:18 +01:00
media add more examples to README 2021-03-17 22:58:10 +01:00
test Merge character paths and join them with random lines 2021-02-03 20:39:28 +01:00
.editorconfig [UPDATED] refactor so that user can load a new font 2017-01-04 21:11:47 +08:00
.gitignore Merge character paths and join them with random lines 2021-02-03 20:39:28 +01:00
.npmignore [FIXED] .npmignore added to prevent git file being installed when depending from github over npm 2019-05-03 21:31:06 +08:00
.travis.yml [UPDATED] test for node version 8.x 2017-06-19 16:25:28 +08:00
HISTORY.md Merge character paths and join them with random lines 2021-02-03 20:39:28 +01:00
index.d.ts add and document options for manipulating path 2021-02-03 22:13:23 +01:00
index.js [refactor] use opentype.js directly with a 3d font 2016-07-01 14:01:37 +08:00
LICENSE.md use matrix transformation to replace individual transform 2016-05-31 09:38:32 +08:00
package-lock.json Merge character paths and join them with random lines 2021-02-03 20:39:28 +01:00
package.json change urls to git.pixelplanet.fun 2022-06-18 11:33:57 +02:00
README_CN.md Remove noise lines 2021-02-03 09:43:18 +01:00
README.md add support for font option and multiple fonts 2021-08-21 16:15:56 +02:00

ppfun-captcha

NPM Version

Generate single-path svg captcha in node.js

sample image

default captcha image:

image

with using fill instead of stroke:

image2

galary of generated captchas (bulk converted to png to display):

image3 image4

Origin

Credits go to the original author produck. This is a fork with added features and fixes for svg-captcha which didn't merge important Bugfixes and is at the current published state cracked. If you want the original svg-capcha, check out svg-captcha-fixed.

useful if you

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

install

npm install ppfun-captcha

usage

var svgCaptcha = require('ppfun-captcha');

var captcha = svgCaptcha.create();
console.log(captcha);
// {data: '<svg.../svg>', text: 'abcd'}

with express

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)

Get a random string of four characters and corresponding svg captcha.

  • size: 4 // size of random string
  • ignoreChars: '0o1i' // filter out some characters like 0o1i
  • background: '#cc9966' // background color of the svg image
  • stroke: 'black' // style/color of the svg path stroke
  • fill: 'black' // style/color of the svg strokes fill
  • style: '' // svg path style attribute, i.e.: 'stroke-width: 4;'
  • font: Font object (see svgCaptcha.loadFont) or Array of multiple Font objects

Options that change the variation of the path:

  • `nodeDeviation: 0.1
  • truncateLineProbability: 0.5
  • truncateCurveProbability: 0.5
  • truncateCurvePositionMin: 0.4
  • truncateCurvePositionMax: 0.6
  • connectionPathDeviation : 10.0

(when using fill, set connectionPathDeviation far lower as it covers parts of the paths and is harder for humans to read - and consider converting it to PNG before serving, to stop bots from being able to read those obstructed paths)

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.createCaptcha(text, options)

return a svg captcha based on text provided.
Options same as create. Returns svg string.

svgCaptcha.loadFont(url)

Load your own font.

  • url: string // path to your font Returns a Font object which you can use in options. (this api is a wrapper around loadFont api of opentype.js) Your may need experiment around various options to make your own font accessible.

svgCaptcha.options

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

In addition to size, 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

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

FAQ

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

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

Why no noise line option?

Noise lines can be easily filtered by path length and we decided to rather join characters with random paths, in order to to make the whole svg one single continuous path, which has the same purpose.

Why no colors?

Colors don't add any security and can be easily changed to black & white anyway. The color of the stroke and fill can be manually choosen if it is neccessary for custom styles.

License

MIT