README.md
1  2 3 <div align="center"> 4 5 [](https://travis-ci.org/lemonce/svg-captcha) 6 [](https://www.npmjs.com/package/svg-captcha) 7 [](https://www.npmjs.com/package/svg-captcha) 8 9 </div> 10 11 > generate svg captcha in node.js 12 13 ## Translations 14 [δΈζ](README_CN.md) 15 16 ## useful if you 17 18 - cannot or do not want to use google recaptcha 19 - have issue with install c++ addon 20 21 ## install 22 ``` 23 npm install --save svg-captcha 24 ``` 25 26 ## usage 27 ```Javascript 28 var svgCaptcha = require('svg-captcha'); 29 30 var captcha = svgCaptcha.create(); 31 console.log(captcha); 32 // {data: '<svg.../svg>', text: 'abcd'} 33 ``` 34 with express 35 ```Javascript 36 var svgCaptcha = require('svg-captcha'); 37 38 app.get('/captcha', function (req, res) { 39 var captcha = svgCaptcha.create(); 40 req.session.captcha = captcha.text; 41 42 res.type('svg'); 43 res.status(200).send(captcha.data); 44 }); 45 ``` 46 47 ## API 48 49 #### `svgCaptcha.create(options)` 50 If no option is passed, you will get a random string of four characters and corresponding svg. 51 52 * `size`: 4 // size of random string 53 * `ignoreChars`: '0o1i' // filter out some characters like 0o1i 54 * `noise`: 1 // number of noise lines 55 * `color`: true // characters will have distinct colors instead of grey, true if background option is set 56 * `background`: '#cc9966' // background color of the svg image 57 58 This function returns an object that has the following property: 59 * `data`: string // svg path data 60 * `text`: string // captcha text 61 62 #### `svgCaptcha.createMathExpr(options)` 63 Similar to create api, you have the above options plus 3 additional: 64 * `mathMin`: 1 // the minimum value the math expression can be 65 * `mathMax`: 9 // the maximum value the math expression can be 66 * `mathOperator`: + // The operator to use, `+`, `-` or `+-` (for random `+` or `-`) 67 68 This function returns an object that has the following property: 69 * `data`: string // svg of the math expression 70 * `text`: string // the answer of the math expression 71 72 #### `svgCaptcha.loadFont(url)` 73 Load your own font and override the default font. 74 * `url`: string // path to your font 75 This api is a wrapper around loadFont api of opentype.js. 76 Your may need experiment around various options to make your own font accessible. 77 See the following api. 78 79 #### `svgCaptcha.options` 80 Gain access to global setting object. 81 It is used for create and createMathExpr api as the default options. 82 83 In addition to size, noise, color, and background, you can also set the following property: 84 * `width`: number // width of captcha 85 * `height`: number // height of captcha 86 * `fontSize`: number // captcha text size 87 * `charPreset`: string // random character preset 88 89 #### `svgCaptcha.randomText([size|options])` 90 return a random string. 91 #### `svgCaptcha(text, options)` 92 return a svg captcha based on text provided. 93 94 In pre 1.1.0 version you have to call these two functions, 95 now you can call create() to save some key strokes ;). 96 97 ## sample image 98 default captcha image: 99 100  101 102 math expression image with color options: 103 104  105 106 ## why use svg? 107 108 It does not require any c++ addon. 109 The result image is smaller than jpeg image. 110 111 > This has to be a joke. /\<text.+\>;.+\<\/text\>/g.test... 112 113 svg captcha uses opentype.js underneath, which means that there is no 114 '<text>1234</text>'. 115 You get 116 '<path fill="#444" d="M104.83 19.74L107.85 19.74L112 33.56L116.13 19.74L119.15 19.74L113.48 36.85...' 117 instead. 118 119 Even though you can write a program that convert svg to png, svg captcha has done its job 120 ββ make captcha recognition harder 121 122 ## License 123 [MIT](LICENSE.md)