Remove noise lines

(they are very distinc paths in length and size and can easily be filtered out by any script, they do not benefit us)
This commit is contained in:
HF 2021-02-03 09:43:18 +01:00
parent 16c4e9ac99
commit a5257ace35
5 changed files with 4 additions and 32 deletions

View File

@ -51,7 +51,6 @@ If no option is passed, you will get a random string of four characters and corr
* `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
@ -80,7 +79,7 @@ See the following api.
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:
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

View File

@ -51,7 +51,6 @@ app.get('/captcha', function (req, res) {
* `size`: 4 // 验证码长度
* `ignoreChars`: '0o1i' // 验证码字符中排除 0o1i
* `noise`: 1 // 干扰线条的数量
* `color`: true // 验证码的字符是否有颜色,默认没有,如果设定了背景,则默认有
* `background`: '#cc9966' // 验证码图片背景颜色
@ -74,7 +73,7 @@ text 属性上是算数式的结果。不过用法和之前是完全一样的。
这是全局配置对象。
create和createMathExpr接口的默认配置就是使用的这个对象。
除了 size, noise, color, 和 background 之外,你还可以修改以下属性:
除了 size, color, 和 background 之外,你还可以修改以下属性:
* `width`: number // width of captcha
* `height`: number // height of captcha
* `fontSize`: number // captcha text size
@ -106,4 +105,4 @@ create和createMathExpr接口的默认配置就是使用的这个对象。
这样的路径没有text标签。所以SVG验证码可能比的图片普通验证码要更难识别因为你必须先做SVG到其它格式的转化。
## License
[MIT](LICENSE.md)
[MIT](LICENSE.md)

5
index.d.ts vendored
View File

@ -41,11 +41,6 @@ declare class ConfigObject {
* filter out some characters
*/
ignoreChars?: string;
/**
* default: 1
* number of noise lines
*/
noise?: number;
/**
* default: white
* background color of svg image

View File

@ -5,25 +5,6 @@ const optionMngr = require('./option-manager');
const opts = optionMngr.options;
const getLineNoise = function (width, height, options) {
const hasColor = options.color;
const noiseLines = [];
const min = options.inverse ? 7 : 1;
const max = options.inverse ? 15 : 9;
let i = -1;
while (++i < options.noise) {
const start = `${random.int(1, 21)} ${random.int(1, height - 1)}`;
const end = `${random.int(width - 21, width - 1)} ${random.int(1, height - 1)}`;
const mid1 = `${random.int((width / 2) - 21, (width / 2) + 21)} ${random.int(1, height - 1)}`;
const mid2 = `${random.int((width / 2) - 21, (width / 2) + 21)} ${random.int(1, height - 1)}`;
const color = hasColor ? random.color() : random.greyColor(min, max);
noiseLines.push(`<path d="M${start} C${mid1},${mid2},${end}" stroke="${color}" fill="none"/>`);
}
return noiseLines;
};
const getText = function (text, width, height, options) {
const len = text.length;
const spacing = (width - 2) / (len + 1);
@ -58,8 +39,7 @@ const createCaptcha = function (text, options) {
const bgRect = bg ?
`<rect width="100%" height="100%" fill="${bg}"/>` : '';
const paths =
[].concat(getLineNoise(width, height, options))
.concat(getText(text, width, height, options))
[].concat(getText(text, width, height, options))
.sort(() => Math.random() - 0.5)
.join('');
const start = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0,0,${width},${height}">`;

View File

@ -11,7 +11,6 @@ const descender = font.descender;
const options = {
width: 150,
height: 50,
noise: 1,
color: false,
background: '',
size: 4,