From 91ed2ff627108f98cb7afdab5054a3e4c7df622f Mon Sep 17 00:00:00 2001 From: Shreyash Raj Date: Sat, 23 Oct 2021 17:16:13 +0530 Subject: [PATCH] Update - removed publish command yml from .github/workflows - Update in docs folder - Removed js-script - Updated extra.ts file - Defined a new type --- .github/workflows/npm-publish.yml | 47 -- docs/Captcha.html | 1226 +++++++++++++++++++++++++++++ docs/CaptchaGenerator.js.html | 2 +- docs/captcha.js.html | 15 +- docs/constants.js.html | 11 +- docs/extra.js.html | 49 +- docs/global.html | 240 +++++- js-script/CaptchaGenerator.d.ts | 131 --- js-script/CaptchaGenerator.js | 177 ----- js-script/captcha.d.ts | 61 -- js-script/captcha.js | 140 ---- js-script/constants.d.ts | 64 -- js-script/constants.js | 65 -- js-script/extra.d.ts | 35 - js-script/extra.js | 52 -- js-script/index.d.ts | 5 - js-script/index.js | 13 - js-script/util.d.ts | 3 - js-script/util.js | 31 - test.js | 10 - ts-script/constants.ts | 18 + ts-script/extra.ts | 13 +- 22 files changed, 1519 insertions(+), 889 deletions(-) delete mode 100644 .github/workflows/npm-publish.yml create mode 100644 docs/Captcha.html delete mode 100644 js-script/CaptchaGenerator.d.ts delete mode 100644 js-script/CaptchaGenerator.js delete mode 100644 js-script/captcha.d.ts delete mode 100644 js-script/captcha.js delete mode 100644 js-script/constants.d.ts delete mode 100644 js-script/constants.js delete mode 100644 js-script/extra.d.ts delete mode 100644 js-script/extra.js delete mode 100644 js-script/index.d.ts delete mode 100644 js-script/index.js delete mode 100644 js-script/util.d.ts delete mode 100644 js-script/util.js delete mode 100644 test.js diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml deleted file mode 100644 index aa47c4e..0000000 --- a/.github/workflows/npm-publish.yml +++ /dev/null @@ -1,47 +0,0 @@ -# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created -# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages - -name: Node.js Package - -on: - release: - types: [created] - -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 12 - - run: npm ci - - run: npm test - - publish-npm: - needs: build - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 12 - registry-url: https://registry.npmjs.org/ - - run: npm ci - - run: npm publish - env: - NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} - - publish-gpr: - needs: build - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 12 - registry-url: https://npm.pkg.github.com/ - - run: npm ci - - run: npm publish - env: - NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/docs/Captcha.html b/docs/Captcha.html new file mode 100644 index 0000000..3a30fa3 --- /dev/null +++ b/docs/Captcha.html @@ -0,0 +1,1226 @@ + + + + + + + + Captcha + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + +
+
+
+ +
+
+
+

Class

+

Captcha

+
+ + + + + +
+ +
+ +

Captcha(widthopt, heightopt)

+ +

Captcha Generator

+ + +
+ +
+
+ + +
+
+
+
+ Constructor +
+ + + + +

+ # + + + + new Captcha(widthopt, heightopt) + + +

+ + + + +
+

Start captcha image creation.

+
+ + + + + + + + + + +
Parameters:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
width + + +number + + + + + + <optional>
+ + + + + +

Width of captcha image.

height + + +number + + + + + + <optional>
+ + + + + +

Height of captcha image.

+
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ View Source + + captcha.js, line 10 + +

+ +
+ + + + + + + + + + + + + + + + + + + + + + +
+
+
+ + +
+ + + + + + + + + + + + + + +
+

Members

+
+ +
+ +

+ # + + + png + + +

+ + + + +
+

Get png image of captcha.

+
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ View Source + + captcha.js, line 43 + +

+ +
+ + + + + +
+ +
+ +

+ # + + + text + + +

+ + + + +
+

Get Captcha text.

+
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ View Source + + captcha.js, line 36 + +

+ +
+ + + + + +
+ +
+
+ + + +
+

Methods

+
+ +
+ + + +

+ # + + + + addDecoy(decoyOptionopt) → {Captcha} + + +

+ + + + +
+

Add decoy on your captcha image.

+
+ + + + + + + + + + +
Parameters:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
decoyOption + + +SetDecoyOptions + + + + + + <optional>
+ + + + + +

Decoy option you want to customise

+
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ View Source + + captcha.js, line 61 + +

+ +
+ + + + + + + + + + + + + + + + + + +
+
+
+ + + +
+ + +
+ + +Captcha + + +
+ +
+ + +
+
+ + + + +
+ +
+ + + +

+ # + + + + drawCaptcha(captchaOptionopt) → {Captcha} + + +

+ + + + +
+

Draw captcha text on captcha image.

+
+ + + + + + + + + + +
Parameters:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
captchaOption + + +SetCaptchaOptions + + + + + + <optional>
+ + + + + +
+
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ View Source + + captcha.js, line 102 + +

+ +
+ + + + + + + + + + + + + + + + + + +
+
+
+ + + +
+ + +
+ + +Captcha + + +
+ +
+ + +
+
+ + + + +
+ +
+ + + +

+ # + + + + drawImage(image) → {Captcha} + + +

+ + + + +
+

Draw image on your captcha.

+
+ + + + + + + + + + +
Parameters:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
image + + +Image + + + +

Choose image you want to add.

+
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ View Source + + captcha.js, line 52 + +

+ +
+ + + + + + + + + + + + + + + + + + +
+
+
+ + + +
+ + +
+ + +Captcha + + +
+ +
+ + +
+
+ + + + +
+ +
+ + + +

+ # + + + + drawTrace(traceOptionopt) → {Captcha} + + +

+ + + + +
+

Draw trace line over your captcha.

+

Note: If you want to use custom text or change size of captcha text then drawCaptcha before drawTrace.

+
+ + + + + + + + + + +
Parameters:
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
traceOption + + +SetTraceOptions + + + + + + <optional>
+ + + + + +
+
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ View Source + + captcha.js, line 81 + +

+ +
+ + + + + + + + + + + + + + + + + + +
+
+
+ + + +
+ + +
+ + +Captcha + + +
+ +
+ + +
+
+ + + + +
+ +
+
+ + + + + +
+ +
+ + + + +
+ + + +
+
+
+
+ + + + + + \ No newline at end of file diff --git a/docs/CaptchaGenerator.js.html b/docs/CaptchaGenerator.js.html index cdf2739..4f53136 100644 --- a/docs/CaptchaGenerator.js.html +++ b/docs/CaptchaGenerator.js.html @@ -110,7 +110,7 @@

CaptchaGenerator.js

this.captcha = constants_1.defaultCaptchaOption; this.trace = constants_1.defaultTraceOptions; this.decoy = constants_1.defaultDecoyOptions; - this.captcha.text = (0, util_1.randomText)(this.captcha.characters); + this.captcha.text = (0, util_1.randomText)(this.captcha.characters || 6); } /** * Get the text of captcha. diff --git a/docs/captcha.js.html b/docs/captcha.js.html index 42d544e..6b74fc5 100644 --- a/docs/captcha.js.html +++ b/docs/captcha.js.html @@ -125,7 +125,7 @@

captcha.js

} /** * Get png image of captcha. - * @returns {Buffer} Get png image of captcha created. + * @returns {Buffer | Promise<Buffer>} Get png image of captcha created. */ get png() { this._canvas.async = this.async; @@ -137,7 +137,7 @@

captcha.js

* @returns {Captcha} */ drawImage(image) { - this._ctx.drawImage(image, 0, 0); + this._ctx.drawImage(image, 0, 0, this._width, this._height); return this; } /** @@ -192,9 +192,9 @@

captcha.js

if (captchaOption.text) option.characters = captchaOption.text.length; if (!captchaOption.text && captchaOption.characters) - option.text = (0, util_1.randomText)(option.characters); + option.text = (0, util_1.randomText)(option.characters || 6); if (!option.text) - option.text = (0, util_1.randomText)(option.characters); + option.text = (0, util_1.randomText)(option.characters || 6); this._captcha = option; if (!this._coordinates[0]) this._coordinates = (0, util_1.getRandomCoordinate)(this._height, this._width, option.characters || 6); @@ -208,10 +208,10 @@

captcha.js

if (option.skew) { this._ctx.transform(1, Math.random(), (0, util_1.getRandom)(20) / 100, 1, 0, 0); } - if (option.rotate > 0) { + if (option.rotate && option.rotate > 0) { this._ctx.rotate((0, util_1.getRandom)(-option.rotate, option.rotate) * Math.PI / 180); } - if (((_a = option.colors) === null || _a === void 0 ? void 0 : _a.length) > 2) { + if (option.colors && ((_a = option.colors) === null || _a === void 0 ? void 0 : _a.length) > 2) { this._ctx.fillStyle = option.colors[(0, util_1.getRandom)(option.colors.length - 1)]; } this._ctx.fillText(option.text[n], 0, 0); @@ -220,6 +220,9 @@

captcha.js

; return this; } + toBuffer() { + this._canvas.toBuffer('png'); + } } exports.Captcha = Captcha; diff --git a/docs/constants.js.html b/docs/constants.js.html index a588f31..07e9330 100644 --- a/docs/constants.js.html +++ b/docs/constants.js.html @@ -87,7 +87,7 @@

constants.js

"use strict";
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.defaultDimension = exports.defaultDecoyOptions = exports.defaultTraceOptions = exports.defaultCaptchaOption = void 0;
+exports.CreateCaptchaOptions = exports.defaultDimension = exports.defaultDecoyOptions = exports.defaultTraceOptions = exports.defaultCaptchaOption = void 0;
 ;
 ;
 ;
@@ -150,6 +150,15 @@ 

constants.js

height: 100, width: 300 }; +/** + * Create captcha options in functions. + * @typedef CreateCaptchaOptions + * @property {SetCaptchaOptions} [captcha] Captcha text options to customise text appearance and value. + * @property {SetDecoyOptions} [decoy] + * @property {SetTraceOptions} [trace] + * @property {Image} [background] + */ +exports.CreateCaptchaOptions = {};
diff --git a/docs/extra.js.html b/docs/extra.js.html index 47a2600..9d02d01 100644 --- a/docs/extra.js.html +++ b/docs/extra.js.html @@ -89,30 +89,25 @@

extra.js

Object.defineProperty(exports, "__esModule", { value: true }); exports.createCaptchaSync = exports.createCaptcha = void 0; const _1 = require("."); -const captchaValue = {}; /** * Create custom captcha from scratch. * @async * @param {number} width Width of captcha image. * @param {number} height Height of captcha image. - * @param {string} [text] Captcha text. + * @param {CreateCaptchaOptions} [option] Captcha text. * @returns */ -function createCaptcha(width, height, text) { +function createCaptcha(width, height, option = {}) { const captcha = new _1.Captcha(width, height); const decoyCount = Math.floor(width * height / 2500); - captcha.addDecoy({ - total: decoyCount, - opacity: 1 - }); - if (text) { - captcha.drawCaptcha({ text: text }); - } - else { - captcha.drawCaptcha(); - text = captcha.text; - } - captcha.drawTrace(); + if (!option.decoy) + option.decoy = {}; + if (!option.decoy.total) + option.decoy.total = decoyCount; + captcha.addDecoy(option.decoy); + captcha.drawCaptcha(option.captcha); + const text = captcha.text; + captcha.drawTrace(option.trace); captcha.addDecoy({ opacity: 1 }); return { image: captcha.png, text: captcha.text }; } @@ -122,25 +117,21 @@

extra.js

* Create captcha in sync mode. * @param {number} width captcha image width. * @param {number} height captcha image height. - * @param {string} [text] Captcha text. + * @param {CreateCaptchaOptions} [option] Captcha text. * @returns */ -function createCaptchaSync(width, height, text) { +function createCaptchaSync(width, height, option = {}) { const captcha = new _1.Captcha(width, height); const decoyCount = Math.floor(width * height / 2500); captcha.async = false; - captcha.addDecoy({ - total: decoyCount, - opacity: 1 - }); - if (text) { - captcha.drawCaptcha({ text: text }); - } - else { - captcha.drawCaptcha(); - text = captcha.text; - } - captcha.drawTrace(); + if (!option.decoy) + option.decoy = {}; + if (!option.decoy.total) + option.decoy.total = decoyCount; + captcha.addDecoy(option.decoy); + captcha.drawCaptcha(option.captcha); + const text = captcha.text; + captcha.drawTrace(option.trace); captcha.addDecoy({ opacity: 1 }); return { image: captcha.png, text: captcha.text }; } diff --git a/docs/global.html b/docs/global.html index 0b91d3b..ac8e81f 100644 --- a/docs/global.html +++ b/docs/global.html @@ -169,7 +169,7 @@

- createCaptcha(width, height, textopt) + createCaptcha(width, height, optionopt)

@@ -284,13 +284,13 @@
Parameters:
- text + option -string +CreateCaptchaOptions @@ -358,7 +358,7 @@
Parameters:

View Source - extra.js, line 14 + extra.js, line 13

@@ -410,7 +410,7 @@

- createCaptchaSync(width, height, textopt) + createCaptchaSync(width, height, optionopt)

@@ -525,13 +525,13 @@
Parameters:
- text + option -string +CreateCaptchaOptions @@ -599,7 +599,7 @@
Parameters:

View Source - extra.js, line 41 + extra.js, line 36

@@ -653,6 +653,230 @@

Type Definitions

+

+ # + + + CreateCaptchaOptions + + +

+ + + + +
+

Create captcha options in functions.

+
+ + + + + +
Properties:
+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDescription
captcha + + +SetCaptchaOptions + + + + + + <optional>
+ + + +

Captcha text options to customise text appearance and value.

decoy + + +SetDecoyOptions + + + + + + <optional>
+ + + +
trace + + +SetTraceOptions + + + + + + <optional>
+ + + +
background + + +Image + + + + + + <optional>
+ + + +
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ View Source + + constants.js, line 66 + +

+ +
+ + + + + +
+ +
+

# diff --git a/js-script/CaptchaGenerator.d.ts b/js-script/CaptchaGenerator.d.ts deleted file mode 100644 index 7f17fe2..0000000 --- a/js-script/CaptchaGenerator.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -/// -import { Image } from "skia-canvas"; -import { SetCaptchaOption, SetDecoyOption, SetTraceOption } from "./constants"; -/** - * Captcha generator class. - */ -export declare class CaptchaGenerator { - private height; - private width; - private captcha; - private trace; - private decoy; - private background?; - /** - * Initatiates the creation of captcha image generation. - * @example const captcha = new CaptchaGenerator({height: 200, width: 600}); - * @param {object} [options] Options for constructor. - * @param {integer} [options.height=100] Height of captcha image. - * @param {integer} [options.width=300] Width of captcha image. - * @since 2.0.0 - */ - constructor(options?: { - height: number; - width: number; - }); - /** - * Get the text of captcha. - * @type {string} - * @since 2.0.3 - */ - get text(): string | undefined; - /** - * set dimension for your captcha image - * @param {integer} height Height of captcha image. - * @param {integer} width Width of captcha image. - * @example - * const { CaptchaGenerator } = require("captcha-canvas"); - * const fs = require("fs") - * const captcha = new CaptchaGenerator(); - * captcha.setDimension(200, 600); - * const buffer = await captcha.generate() //generate image - * - * fs.writeFileSync("image.png", buffer) - * @since 2.0.0 - */ - setDimension(height: number, width: number): CaptchaGenerator; - /** - * Set background for captcha image. - * @param {buffer} image Buffer/url/path of image. - * @example - * const { CaptchaGenerator } = require("captcha-canvas"); - * const fs = require("fs") - * const captcha = new CaptchaGenerator(); - * captcha.setBackground("./path/toFile"); - * const buffer = await captcha.generate() //generate image - * - * fs.writeFileSync("image.png", buffer) - * @since 2.0.0 - */ - setBackground(image: string | Buffer): this; - /** - * Change captcha text options - * @param {SetCaptchaOptions} options Captcha appearance options. - * @example - * const { CaptchaGenerator } = require("captcha-canvas"); - * const fs = require("fs") - * const captcha = new CaptchaGenerator(); - * const options = {font: "Comic Sans", size: 60} - * captcha.setCaptcha(options) - * const buffer = await captcha.generate() //generate image - * - * fs.writeFileSync("image.png", buffer) - * @since 2.0.0 - */ - setCaptcha(option: SetCaptchaOption): this; - /** - * Change trace creation options. - * @param {SetTraceOptions} options Trace Line appearance options. - * @example - * const { CaptchaGenerator } = require("captcha-canvas"); - * const fs = require("fs") - * const captcha = new CaptchaGenerator(); - * const options = {size: 5, color: "deeppink"} - * captcha.setTrace(options) - * const buffer = await captcha.generate() //generate image - * - * fs.writeFileSync("image.png", buffer) - * @since 2.0.0 - */ - setTrace(option: SetTraceOption): this; - /** - * Change decoy options - * @param {SetDecoyOptions} options Decoy characters customisation options - * @since 2.0.0 - */ - setDecoy(option: SetDecoyOption): this; - /** - * Method which returns image buffer - * @async - * @returns {Promise} - * @example - * const { CaptchaGenerator } = require("captcha-canvas"); - * const fs = require("fs") - * const captcha = new CaptchaGenerator(); - * const buffer = await captcha.generate() //generate image - * - * fs.writeFileSync("image.png", buffer) - * @since 2.0.0 - */ - generate(): Promise; - /** - * Non asynchronous method to generate captcha image. - * > Note: It do not use `setBackground` method value for background image. If you want to set background - * and also use generateSync method then use background option in genrateSync method. - * @param {object} [options] Options to add extra values - * @param {Image} [options.background] Add background image. - * @example - * const { CaptchaGenerator, resolveImage } = require("captcha-canvas"); - * const fs = require("fs"); - * const img = await resolveImage("./path/to/file"); - * - * const captcha = new CaptchaGenerator() - * .generateSync({background: img}); - * - * fs.writeFileSync("image.png", captcha); - * @since 2.2.0 - */ - generateSync(option?: { - background?: Image; - }): Buffer; -} diff --git a/js-script/CaptchaGenerator.js b/js-script/CaptchaGenerator.js deleted file mode 100644 index 54563fa..0000000 --- a/js-script/CaptchaGenerator.js +++ /dev/null @@ -1,177 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.CaptchaGenerator = void 0; -const skia_canvas_1 = require("skia-canvas"); -const _1 = require("."); -const constants_1 = require("./constants"); -const util_1 = require("./util"); -/** - * Captcha generator class. - */ -class CaptchaGenerator { - /** - * Initatiates the creation of captcha image generation. - * @example const captcha = new CaptchaGenerator({height: 200, width: 600}); - * @param {object} [options] Options for constructor. - * @param {integer} [options.height=100] Height of captcha image. - * @param {integer} [options.width=300] Width of captcha image. - * @since 2.0.0 - */ - constructor(options = { height: 100, width: 300 }) { - this.height = options.height; - this.width = options.width; - this.captcha = constants_1.defaultCaptchaOption; - this.trace = constants_1.defaultTraceOptions; - this.decoy = constants_1.defaultDecoyOptions; - this.captcha.text = (0, util_1.randomText)(this.captcha.characters || 6); - } - /** - * Get the text of captcha. - * @type {string} - * @since 2.0.3 - */ - get text() { - return this.captcha.text; - } - /** - * set dimension for your captcha image - * @param {integer} height Height of captcha image. - * @param {integer} width Width of captcha image. - * @example - * const { CaptchaGenerator } = require("captcha-canvas"); - * const fs = require("fs") - * const captcha = new CaptchaGenerator(); - * captcha.setDimension(200, 600); - * const buffer = await captcha.generate() //generate image - * - * fs.writeFileSync("image.png", buffer) - * @since 2.0.0 - */ - setDimension(height, width) { - this.height = height; - this.width = width; - return this; - } - /** - * Set background for captcha image. - * @param {buffer} image Buffer/url/path of image. - * @example - * const { CaptchaGenerator } = require("captcha-canvas"); - * const fs = require("fs") - * const captcha = new CaptchaGenerator(); - * captcha.setBackground("./path/toFile"); - * const buffer = await captcha.generate() //generate image - * - * fs.writeFileSync("image.png", buffer) - * @since 2.0.0 - */ - setBackground(image) { - this.background = image; - return this; - } - /** - * Change captcha text options - * @param {SetCaptchaOptions} options Captcha appearance options. - * @example - * const { CaptchaGenerator } = require("captcha-canvas"); - * const fs = require("fs") - * const captcha = new CaptchaGenerator(); - * const options = {font: "Comic Sans", size: 60} - * captcha.setCaptcha(options) - * const buffer = await captcha.generate() //generate image - * - * fs.writeFileSync("image.png", buffer) - * @since 2.0.0 - */ - setCaptcha(option) { - this.captcha = { ...this.captcha, ...option }; - if (option.text) - this.captcha.characters = option.text.length; - if (!option.text && option.characters) - this.captcha.text = (0, util_1.randomText)(option.characters); - return this; - } - /** - * Change trace creation options. - * @param {SetTraceOptions} options Trace Line appearance options. - * @example - * const { CaptchaGenerator } = require("captcha-canvas"); - * const fs = require("fs") - * const captcha = new CaptchaGenerator(); - * const options = {size: 5, color: "deeppink"} - * captcha.setTrace(options) - * const buffer = await captcha.generate() //generate image - * - * fs.writeFileSync("image.png", buffer) - * @since 2.0.0 - */ - setTrace(option) { - this.trace = { ...this.trace, ...option }; - return this; - } - /** - * Change decoy options - * @param {SetDecoyOptions} options Decoy characters customisation options - * @since 2.0.0 - */ - setDecoy(option) { - this.decoy = { ...this.decoy, ...option }; - return this; - } - /** - * Method which returns image buffer - * @async - * @returns {Promise} - * @example - * const { CaptchaGenerator } = require("captcha-canvas"); - * const fs = require("fs") - * const captcha = new CaptchaGenerator(); - * const buffer = await captcha.generate() //generate image - * - * fs.writeFileSync("image.png", buffer) - * @since 2.0.0 - */ - async generate() { - const captchaCanvas = new _1.Captcha(this.width, this.height); - if (this.background) - captchaCanvas.drawImage(await (0, skia_canvas_1.loadImage)(this.background)); - if (this.decoy.opacity) - captchaCanvas.addDecoy(this.decoy); - if (this.captcha.opacity) - captchaCanvas.drawCaptcha(this.captcha); - if (this.trace.opacity) - captchaCanvas.drawTrace(this.trace); - return captchaCanvas.png; - } - /** - * Non asynchronous method to generate captcha image. - * > Note: It do not use `setBackground` method value for background image. If you want to set background - * and also use generateSync method then use background option in genrateSync method. - * @param {object} [options] Options to add extra values - * @param {Image} [options.background] Add background image. - * @example - * const { CaptchaGenerator, resolveImage } = require("captcha-canvas"); - * const fs = require("fs"); - * const img = await resolveImage("./path/to/file"); - * - * const captcha = new CaptchaGenerator() - * .generateSync({background: img}); - * - * fs.writeFileSync("image.png", captcha); - * @since 2.2.0 - */ - generateSync(option = {}) { - const captchaCanvas = new _1.Captcha(this.width, this.height); - captchaCanvas.async = false; - if (option.background) - captchaCanvas.drawImage(option.background); - if (this.decoy.opacity) - captchaCanvas.addDecoy(this.decoy); - if (this.captcha.opacity) - captchaCanvas.drawCaptcha(this.captcha); - if (this.trace.opacity) - captchaCanvas.drawTrace(this.trace); - return captchaCanvas.png; - } -} -exports.CaptchaGenerator = CaptchaGenerator; diff --git a/js-script/captcha.d.ts b/js-script/captcha.d.ts deleted file mode 100644 index 01518ca..0000000 --- a/js-script/captcha.d.ts +++ /dev/null @@ -1,61 +0,0 @@ -/// -import { Canvas, CanvasRenderingContext2D, Image } from "skia-canvas"; -import { SetCaptchaOption, SetDecoyOption, SetTraceOption } from "./constants"; -/** - * Captcha Generator - */ -export declare class Captcha { - protected _height: number; - protected _width: number; - protected _captcha: SetCaptchaOption; - protected _trace: SetTraceOption; - protected _decoy: SetDecoyOption; - protected _canvas: Canvas; - protected _ctx: CanvasRenderingContext2D; - protected _coordinates: number[][]; - async: Boolean; - /** - * Start captcha image creation. - * @param {number} [width] Width of captcha image. - * @param {number} [height] Height of captcha image. - * @constructor - */ - constructor(width?: number, height?: number); - /** - * Get Captcha text. - * @returns {string} Get captcha text. - */ - get text(): string; - /** - * Get png image of captcha. - * @returns {Buffer | Promise} Get png image of captcha created. - */ - get png(): Buffer | Promise; - /** - * Draw image on your captcha. - * @param {Image} image Choose image you want to add. - * @returns {Captcha} - */ - drawImage(image: Image): Captcha; - /** - * Add decoy on your captcha image. - * @param {SetDecoyOptions} [decoyOption] Decoy option you want to customise - * @returns {Captcha} - */ - addDecoy(decoyOption?: SetDecoyOption): Captcha; - /** - * Draw trace line over your captcha. - * - * Note: If you want to use custom text or change size of captcha text then drawCaptcha before drawTrace. - * @param {SetTraceOptions} [traceOption] - * @returns {Captcha} - */ - drawTrace(traceOption?: SetTraceOption): Captcha; - /** - * Draw captcha text on captcha image. - * @param {SetCaptchaOptions} [captchaOption] - * @returns {Captcha} - */ - drawCaptcha(captchaOption?: SetCaptchaOption): Captcha; - toBuffer(): void; -} diff --git a/js-script/captcha.js b/js-script/captcha.js deleted file mode 100644 index ba2ee52..0000000 --- a/js-script/captcha.js +++ /dev/null @@ -1,140 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.Captcha = void 0; -const skia_canvas_1 = require("skia-canvas"); -const constants_1 = require("./constants"); -const util_1 = require("./util"); -/** - * Captcha Generator - */ -class Captcha { - /** - * Start captcha image creation. - * @param {number} [width] Width of captcha image. - * @param {number} [height] Height of captcha image. - * @constructor - */ - constructor(width = constants_1.defaultDimension.width, height = constants_1.defaultDimension.height) { - this._height = height; - this._width = width; - this._captcha = constants_1.defaultCaptchaOption; - this._trace = constants_1.defaultTraceOptions; - this._decoy = constants_1.defaultDecoyOptions; - const canvas = new skia_canvas_1.Canvas(width, height); - const ctx = canvas.getContext('2d'); - ctx.lineJoin = 'miter'; - ctx.textBaseline = 'middle'; - this._canvas = canvas; - this._ctx = ctx; - this.async = true; - this._coordinates = []; - } - /** - * Get Captcha text. - * @returns {string} Get captcha text. - */ - get text() { - return this._captcha.text || ""; - } - /** - * Get png image of captcha. - * @returns {Buffer | Promise} Get png image of captcha created. - */ - get png() { - this._canvas.async = this.async; - return this._canvas.png; - } - /** - * Draw image on your captcha. - * @param {Image} image Choose image you want to add. - * @returns {Captcha} - */ - drawImage(image) { - this._ctx.drawImage(image, 0, 0, this._width, this._height); - return this; - } - /** - * Add decoy on your captcha image. - * @param {SetDecoyOptions} [decoyOption] Decoy option you want to customise - * @returns {Captcha} - */ - addDecoy(decoyOption = {}) { - const option = { ...this._decoy, ...decoyOption }; - if (!option.total) - option.total = Math.floor(this._width * this._height / 10000); - const decoyText = (0, util_1.randomText)(option.total); - this._ctx.font = `${option.size}px ${option.font}`; - this._ctx.globalAlpha = option.opacity; - this._ctx.fillStyle = option.color; - for (let i = 0; i < decoyText.length; i++) { - this._ctx.fillText(decoyText[i], (0, util_1.getRandom)(30, this._width - 30), (0, util_1.getRandom)(30, this._height - 30)); - } - return this; - } - /** - * Draw trace line over your captcha. - * - * Note: If you want to use custom text or change size of captcha text then drawCaptcha before drawTrace. - * @param {SetTraceOptions} [traceOption] - * @returns {Captcha} - */ - drawTrace(traceOption = {}) { - const option = { ...this._trace, ...traceOption }; - if (!this._coordinates[0]) - this._coordinates = (0, util_1.getRandomCoordinate)(this._height, this._width, this._captcha.characters || 6); - const coordinates = this._coordinates; - this._ctx.strokeStyle = option.color; - this._ctx.globalAlpha = option.opacity; - this._ctx.beginPath(); - this._ctx.moveTo(coordinates[0][0], coordinates[0][1]); - this._ctx.lineWidth = option.size; - for (let i = 1; i < coordinates.length; i++) { - this._ctx.lineTo(coordinates[i][0], coordinates[i][1]); - } - this._ctx.stroke(); - return this; - } - /** - * Draw captcha text on captcha image. - * @param {SetCaptchaOptions} [captchaOption] - * @returns {Captcha} - */ - drawCaptcha(captchaOption = {}) { - var _a; - const option = { ...this._captcha, ...captchaOption }; - if (captchaOption.text) - option.characters = captchaOption.text.length; - if (!captchaOption.text && captchaOption.characters) - option.text = (0, util_1.randomText)(option.characters || 6); - if (!option.text) - option.text = (0, util_1.randomText)(option.characters || 6); - this._captcha = option; - if (!this._coordinates[0]) - this._coordinates = (0, util_1.getRandomCoordinate)(this._height, this._width, option.characters || 6); - const coordinates = this._coordinates; - this._ctx.font = `${option.size}px ${option.font}`; - this._ctx.globalAlpha = option.opacity; - this._ctx.fillStyle = option.color; - for (let n = 0; n < coordinates.length; n++) { - this._ctx.save(); - this._ctx.translate(coordinates[n][0], coordinates[n][1]); - if (option.skew) { - this._ctx.transform(1, Math.random(), (0, util_1.getRandom)(20) / 100, 1, 0, 0); - } - if (option.rotate && option.rotate > 0) { - this._ctx.rotate((0, util_1.getRandom)(-option.rotate, option.rotate) * Math.PI / 180); - } - if (option.colors && ((_a = option.colors) === null || _a === void 0 ? void 0 : _a.length) > 2) { - this._ctx.fillStyle = option.colors[(0, util_1.getRandom)(option.colors.length - 1)]; - } - this._ctx.fillText(option.text[n], 0, 0); - this._ctx.restore(); - } - ; - return this; - } - toBuffer() { - this._canvas.toBuffer('png'); - } -} -exports.Captcha = Captcha; diff --git a/js-script/constants.d.ts b/js-script/constants.d.ts deleted file mode 100644 index acb8077..0000000 --- a/js-script/constants.d.ts +++ /dev/null @@ -1,64 +0,0 @@ -export interface SetDimensionOption { - height: number; - width: number; -} -export interface SetCaptchaOption { - characters?: number; - text?: string; - color?: string; - font?: string; - skew?: boolean; - colors?: string[] | []; - rotate?: number; - size?: number; - opacity?: number; -} -export interface SetDecoyOption { - color?: string; - font?: string; - size?: number; - opacity?: number; - total?: number; -} -export interface SetTraceOption { - color?: string; - size?: number; - opacity?: number; -} -/** - * Captcha text options to customise text appearance and value. - * @typedef SetCaptchaOptions - * @property {integer} [characters=6] Length of captcha text. - * @property {string} [text="Random UpperCase string"] Text of captcha - * @property {hexCode} [color="#32cf7e"] Color of captcha text. - * @property {font} [font="Sans"] Font of captcha text. - * @property {boolean} [skew=true] Skew captcha text. - * @property {array} [colors=[]] Array of hexCode will override color property. - * @property {number} [rotate=5] Range of angle to rotate text. - * @property {number} [size=40] Size of captcha text. - * @property {float} [opacity=1] Opcaity of captcha text. - */ -export declare const defaultCaptchaOption: SetCaptchaOption; -/** - * @typedef SetTraceOptions - * @property {hexCode} [color="#32cf7e"] Color of trace line. - * @property {number} [size=3] Width of trace line. - * @property {float} [opacity=1] Opacoty of trace line. - */ -export declare const defaultTraceOptions: SetTraceOption; -/** - * @typedef SetDecoyOptions - * @property {hexCode} [color="#646566"] Color of decoy characters. - * @property {font} [font="Sans"] Font of decoy characters. - * @property {number} [size=20] Size of decoy characters. - * @property {float} [opacity=0.8] Opacity of decoy characters. - * @property {number} [total] Total count of decoy characters. - */ -export declare const defaultDecoyOptions: SetDecoyOption; -/** - * Customise dimension of captcha image. - * @typedef SetDimensionOption - * @property {integer} [height=100] Height of captcha image. - * @property {integer} [width=300] Width of captcha image. - */ -export declare const defaultDimension: SetDimensionOption; diff --git a/js-script/constants.js b/js-script/constants.js deleted file mode 100644 index 63b39a5..0000000 --- a/js-script/constants.js +++ /dev/null @@ -1,65 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.defaultDimension = exports.defaultDecoyOptions = exports.defaultTraceOptions = exports.defaultCaptchaOption = void 0; -; -; -; -; -/** - * Captcha text options to customise text appearance and value. - * @typedef SetCaptchaOptions - * @property {integer} [characters=6] Length of captcha text. - * @property {string} [text="Random UpperCase string"] Text of captcha - * @property {hexCode} [color="#32cf7e"] Color of captcha text. - * @property {font} [font="Sans"] Font of captcha text. - * @property {boolean} [skew=true] Skew captcha text. - * @property {array} [colors=[]] Array of hexCode will override color property. - * @property {number} [rotate=5] Range of angle to rotate text. - * @property {number} [size=40] Size of captcha text. - * @property {float} [opacity=1] Opcaity of captcha text. - */ -exports.defaultCaptchaOption = { - characters: 6, - size: 40, - font: 'Sans', - skew: true, - colors: [], - rotate: 5, - color: '#32cf7e', - opacity: 0.8, -}; -/** - * @typedef SetTraceOptions - * @property {hexCode} [color="#32cf7e"] Color of trace line. - * @property {number} [size=3] Width of trace line. - * @property {float} [opacity=1] Opacoty of trace line. - */ -exports.defaultTraceOptions = { - size: 3, - color: '#32cf7e', - opacity: 1, -}; -/** - * @typedef SetDecoyOptions - * @property {hexCode} [color="#646566"] Color of decoy characters. - * @property {font} [font="Sans"] Font of decoy characters. - * @property {number} [size=20] Size of decoy characters. - * @property {float} [opacity=0.8] Opacity of decoy characters. - * @property {number} [total] Total count of decoy characters. - */ -exports.defaultDecoyOptions = { - color: '#646566', - font: 'Sans', - size: 20, - opacity: 0.8, -}; -/** - * Customise dimension of captcha image. - * @typedef SetDimensionOption - * @property {integer} [height=100] Height of captcha image. - * @property {integer} [width=300] Width of captcha image. - */ -exports.defaultDimension = { - height: 100, - width: 300 -}; diff --git a/js-script/extra.d.ts b/js-script/extra.d.ts deleted file mode 100644 index 72cd062..0000000 --- a/js-script/extra.d.ts +++ /dev/null @@ -1,35 +0,0 @@ -/// -import { Image } from "skia-canvas"; -import { SetCaptchaOption, SetDecoyOption, SetTraceOption } from "./constants"; -interface captchaValueSync { - image: Buffer; - text: string; -} -interface captchaValue { - image: Promise; - text: string; -} -interface CreateCaptchaOptions { - captcha?: SetCaptchaOption; - trace?: SetTraceOption; - decoy?: SetDecoyOption; - background?: Image; -} -/** - * Create custom captcha from scratch. - * @async - * @param {number} width Width of captcha image. - * @param {number} height Height of captcha image. - * @param {string} [text] Captcha text. - * @returns - */ -export declare function createCaptcha(width: number, height: number, option?: CreateCaptchaOptions): captchaValue; -/** - * Create captcha in sync mode. - * @param {number} width captcha image width. - * @param {number} height captcha image height. - * @param {CreateCaptchaOptions} [option] Captcha text. - * @returns - */ -export declare function createCaptchaSync(width: number, height: number, option?: CreateCaptchaOptions): captchaValueSync; -export {}; diff --git a/js-script/extra.js b/js-script/extra.js deleted file mode 100644 index c6108d9..0000000 --- a/js-script/extra.js +++ /dev/null @@ -1,52 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.createCaptchaSync = exports.createCaptcha = void 0; -const _1 = require("."); -/** - * Create custom captcha from scratch. - * @async - * @param {number} width Width of captcha image. - * @param {number} height Height of captcha image. - * @param {string} [text] Captcha text. - * @returns - */ -function createCaptcha(width, height, option = {}) { - const captcha = new _1.Captcha(width, height); - const decoyCount = Math.floor(width * height / 2500); - if (!option.decoy) - option.decoy = {}; - if (!option.decoy.total) - option.decoy.total = decoyCount; - captcha.addDecoy(option.decoy); - captcha.drawCaptcha(option.captcha); - const text = captcha.text; - captcha.drawTrace(option.trace); - captcha.addDecoy({ opacity: 1 }); - return { image: captcha.png, text: captcha.text }; -} -exports.createCaptcha = createCaptcha; -; -/** - * Create captcha in sync mode. - * @param {number} width captcha image width. - * @param {number} height captcha image height. - * @param {CreateCaptchaOptions} [option] Captcha text. - * @returns - */ -function createCaptchaSync(width, height, option = {}) { - const captcha = new _1.Captcha(width, height); - const decoyCount = Math.floor(width * height / 2500); - captcha.async = false; - if (!option.decoy) - option.decoy = {}; - if (!option.decoy.total) - option.decoy.total = decoyCount; - captcha.addDecoy(option.decoy); - captcha.drawCaptcha(option.captcha); - const text = captcha.text; - captcha.drawTrace(option.trace); - captcha.addDecoy({ opacity: 1 }); - return { image: captcha.png, text: captcha.text }; -} -exports.createCaptchaSync = createCaptchaSync; -; diff --git a/js-script/index.d.ts b/js-script/index.d.ts deleted file mode 100644 index f81c149..0000000 --- a/js-script/index.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -export { createCaptcha, createCaptchaSync } from "./extra"; -export { loadImage as resolveImage } from "skia-canvas"; -export { Captcha } from "./captcha"; -export { CaptchaGenerator } from "./CaptchaGenerator"; -export declare const version: string; diff --git a/js-script/index.js b/js-script/index.js deleted file mode 100644 index b8df6f4..0000000 --- a/js-script/index.js +++ /dev/null @@ -1,13 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.version = exports.CaptchaGenerator = exports.Captcha = exports.resolveImage = exports.createCaptchaSync = exports.createCaptcha = void 0; -var extra_1 = require("./extra"); -Object.defineProperty(exports, "createCaptcha", { enumerable: true, get: function () { return extra_1.createCaptcha; } }); -Object.defineProperty(exports, "createCaptchaSync", { enumerable: true, get: function () { return extra_1.createCaptchaSync; } }); -var skia_canvas_1 = require("skia-canvas"); -Object.defineProperty(exports, "resolveImage", { enumerable: true, get: function () { return skia_canvas_1.loadImage; } }); -var captcha_1 = require("./captcha"); -Object.defineProperty(exports, "Captcha", { enumerable: true, get: function () { return captcha_1.Captcha; } }); -var CaptchaGenerator_1 = require("./CaptchaGenerator"); -Object.defineProperty(exports, "CaptchaGenerator", { enumerable: true, get: function () { return CaptchaGenerator_1.CaptchaGenerator; } }); -exports.version = require(`${__dirname}/../package.json`).version; diff --git a/js-script/util.d.ts b/js-script/util.d.ts deleted file mode 100644 index 9709291..0000000 --- a/js-script/util.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -export declare function getRandom(start?: number, end?: number): number; -export declare function randomText(characters: number): string; -export declare function getRandomCoordinate(height: number, width: number, size: number): number[][]; diff --git a/js-script/util.js b/js-script/util.js deleted file mode 100644 index 12c2689..0000000 --- a/js-script/util.js +++ /dev/null @@ -1,31 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.getRandomCoordinate = exports.randomText = exports.getRandom = void 0; -const crypto_1 = require("crypto"); -function getRandom(start = 0, end = 0) { - return Math.round(Math.random() * Math.abs(end - start)) + Math.min(start, end); -} -exports.getRandom = getRandom; -; -function randomText(characters) { - return (0, crypto_1.randomBytes)(characters).toString('hex').toUpperCase().substr(0, characters); -} -exports.randomText = randomText; -; -function getRandomCoordinate(height, width, size) { - let coordinates = []; - for (let i = 0; i < size; i++) { - const widthGap = Math.floor(width / size); - const coordinate = []; - const randomWidth = widthGap * (i + 0.2); - coordinate.push(randomWidth); - const randomHeight = getRandom(30, height - 30); - coordinate.push(randomHeight); - coordinates.push(coordinate); - } - ; - coordinates = coordinates.sort((a, b) => a[0] - b[0]); - return coordinates; -} -exports.getRandomCoordinate = getRandomCoordinate; -; diff --git a/test.js b/test.js deleted file mode 100644 index 7b9ee0d..0000000 --- a/test.js +++ /dev/null @@ -1,10 +0,0 @@ -const { writeFileSync } = require('fs'); -const { createCaptchaSync } = require('./js-script/extra'); - -const captcha = createCaptchaSync(900, 300, { - captcha: { - size: 80, - }, -}); - -writeFileSync('assets/captcha/default.png', captcha.image); \ No newline at end of file diff --git a/ts-script/constants.ts b/ts-script/constants.ts index cbc480e..dd30c19 100644 --- a/ts-script/constants.ts +++ b/ts-script/constants.ts @@ -1,3 +1,5 @@ +import { Image } from "skia-canvas"; + export interface SetDimensionOption { height: number; width: number; @@ -28,6 +30,13 @@ export interface SetTraceOption { size?: number; opacity?: number; }; + +export interface CreateCaptchaOptions { + captcha?: SetCaptchaOption; + trace?: SetTraceOption; + decoy?: SetDecoyOption; + background?: Image; +} /** * Captcha text options to customise text appearance and value. * @typedef SetCaptchaOptions @@ -86,3 +95,12 @@ export const defaultDimension: SetDimensionOption = { height: 100, width: 300 }; +/** + * Create captcha options in functions. + * @typedef CreateCaptchaOptions + * @property {SetCaptchaOptions} [captcha] Captcha text options to customise text appearance and value. + * @property {SetDecoyOptions} [decoy] + * @property {SetTraceOptions} [trace] + * @property {Image} [background] + */ +export const CreateCaptchaOptions = {}; \ No newline at end of file diff --git a/ts-script/extra.ts b/ts-script/extra.ts index d5e1ee6..05388b1 100644 --- a/ts-script/extra.ts +++ b/ts-script/extra.ts @@ -1,6 +1,5 @@ -import { Image } from "skia-canvas"; import { Captcha } from "."; -import { SetCaptchaOption, SetDecoyOption, SetTraceOption } from "./constants"; +import { CreateCaptchaOptions } from "./constants"; interface captchaValueSync { image: Buffer, @@ -12,18 +11,12 @@ interface captchaValue { text: string } -interface CreateCaptchaOptions { - captcha?: SetCaptchaOption; - trace?: SetTraceOption; - decoy?: SetDecoyOption; - background?: Image; -} /** * Create custom captcha from scratch. * @async * @param {number} width Width of captcha image. * @param {number} height Height of captcha image. - * @param {string} [text] Captcha text. + * @param {CreateCaptchaOptions} [option] Captcha text. * @returns */ export function createCaptcha(width: number, height: number, option: CreateCaptchaOptions = {}): captchaValue { @@ -49,7 +42,7 @@ export function createCaptcha(width: number, height: number, option: CreateCaptc * Create captcha in sync mode. * @param {number} width captcha image width. * @param {number} height captcha image height. - * @param {CreateCaptchaOptions} [option] Captcha text. + * @param {CreateCaptchaOptions} [option] Captcha text. * @returns */ export function createCaptchaSync(width: number, height: number, option: CreateCaptchaOptions = {}): captchaValueSync {