Skip to content

Commit

Permalink
v3.1.0
Browse files Browse the repository at this point in the history
- extra function now have all the editing options.
- Small updates in readme
  • Loading branch information
Shashank3736 committed Oct 23, 2021
1 parent 03d2103 commit 433892f
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 100 deletions.
Binary file modified assets/captcha/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion js-script/CaptchaGenerator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export declare class CaptchaGenerator {
* @type {string}
* @since 2.0.3
*/
get text(): string;
get text(): string | undefined;
/**
* set dimension for your captcha image
* @param {integer} height Height of captcha image.
Expand Down
2 changes: 1 addition & 1 deletion js-script/CaptchaGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CaptchaGenerator {
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.
Expand Down
5 changes: 3 additions & 2 deletions js-script/captcha.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export declare class Captcha {
get text(): string;
/**
* 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(): Buffer;
get png(): Buffer | Promise<Buffer>;
/**
* Draw image on your captcha.
* @param {Image} image Choose image you want to add.
Expand All @@ -57,4 +57,5 @@ export declare class Captcha {
* @returns {Captcha}
*/
drawCaptcha(captchaOption?: SetCaptchaOption): Captcha;
toBuffer(): void;
}
15 changes: 9 additions & 6 deletions js-script/captcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Captcha {
}
/**
* 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;
Expand All @@ -50,7 +50,7 @@ class Captcha {
* @returns {Captcha}
*/
drawImage(image) {
this._ctx.drawImage(image, 0, 0);
this._ctx.drawImage(image, 0, 0, this._width, this._height);
return this;
}
/**
Expand Down Expand Up @@ -105,9 +105,9 @@ class Captcha {
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);
Expand All @@ -121,10 +121,10 @@ class Captcha {
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);
Expand All @@ -133,5 +133,8 @@ class Captcha {
;
return this;
}
toBuffer() {
this._canvas.toBuffer('png');
}
}
exports.Captcha = Captcha;
15 changes: 11 additions & 4 deletions js-script/extra.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="node" />
declare const captchaValue: {};
import { Image } from "skia-canvas";
import { SetCaptchaOption, SetDecoyOption, SetTraceOption } from "./constants";
interface captchaValueSync {
image: Buffer;
text: string;
Expand All @@ -8,6 +9,12 @@ interface captchaValue {
image: Promise<Buffer>;
text: string;
}
interface CreateCaptchaOptions {
captcha?: SetCaptchaOption;
trace?: SetTraceOption;
decoy?: SetDecoyOption;
background?: Image;
}
/**
* Create custom captcha from scratch.
* @async
Expand All @@ -16,13 +23,13 @@ interface captchaValue {
* @param {string} [text] Captcha text.
* @returns
*/
export declare function createCaptcha(width: number, height: number, text?: string): captchaValue;
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 {string} [text] Captcha text.
* @param {CreateCaptchaOptions} [option] Captcha text.
* @returns
*/
export declare function createCaptchaSync(width: number, height: number, text?: string): captchaValueSync;
export declare function createCaptchaSync(width: number, height: number, option?: CreateCaptchaOptions): captchaValueSync;
export {};
47 changes: 19 additions & 28 deletions js-script/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCaptchaSync = exports.createCaptcha = void 0;
const _1 = require(".");
const captchaValue = {};
/**
* Create custom captcha from scratch.
* @async
Expand All @@ -11,21 +10,17 @@ const captchaValue = {};
* @param {string} [text] 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 };
}
Expand All @@ -35,25 +30,21 @@ exports.createCaptcha = createCaptcha;
* 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 };
}
Expand Down
106 changes: 104 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "captcha-canvas",
"version": "3.0.4",
"version": "3.1.0",
"description": "A captcha generator by using skia-canvas module.",
"main": "./js-script/index.js",
"files": [
Expand Down Expand Up @@ -32,6 +32,7 @@
"@types/node": "^14.11.8",
"@types/skia-canvas": "^0.9.2",
"better-docs": "^2.3.2",
"canvas": "^2.8.0",
"eslint": "^7.27.0",
"jsdoc": "^3.6.7",
"typescript": "^4.0.2"
Expand Down
19 changes: 10 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict";
exports.__esModule = true;
var fs_1 = require("fs");
var js_script_1 = require("./js-script");
var captcha = new js_script_1.CaptchaGenerator()
.setDimension(200, 200);
console.log(__filename + captcha.text);
(0, fs_1.writeFileSync)('assets/CaptchaGenerator/' + captcha.text + 'captcha.png', captcha.generateSync());
console.log(captcha.text);
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);
9 changes: 0 additions & 9 deletions test.ts

This file was deleted.

Loading

0 comments on commit 433892f

Please sign in to comment.