1.4.0 • Published 7 months ago

persian-captcha-generator v1.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Persian Captcha Generator

A library for generating customizable captchas with Persian numbers and alphabets. This library generates a captcha image with various options such as Persian numbers, alphabets, or both, and adds noise elements like random lines and dots for enhanced complexity.

Features

  • Generate captchas with:
    • Only Persian numbers
    • Only Persian alphabets
    • A mix of Persian numbers and alphabets
  • Customizable:
    • Image size (width and height)
    • Font size and colors
    • Noise elements like lines and dots
  • Outputs a PNG buffer and the text for verification

Installation

Npm:

npm install persian-captcha-generator

Yarn:

yarn add persian-captcha-generator

Usage

NodeJS

import fs from "fs";
import { persianCaptchaGenerator } from "persian-captcha-generator";

(async () => {
  const captcha = await persianCaptchaGenerator({
    length: 6,
    characterSet: "numbers",
    width: 300,
    height: 100,
    fontSize: 40,
    lineCount: 10,
    dotCount: 100,
    textColor: "#000000",
    backgroundColor: "#f8f9fa",
  });

  // Save the PNG buffer as a file
  fs.writeFileSync("captcha.png", captcha.imageBuffer);

  // Log the captcha text for validation
  console.log("Generated Captcha Text:", captcha.text);
})();

ExpressJS

import express from "express";
import { persianCaptchaGenerator } from "persian-captcha-generator";

const app = express();
const PORT = 3000;

app.get("/captcha", async (_req, res) => {
  try {
    const captcha = await persianCaptchaGenerator({
      width: 300,
      height: 100,
      length: 6,
      backgroundColor: "#ffffff",
      textColor: "#000000",
      fontSize: 44,
      lineCount: 8,
      dotCount: 50,
      characterSet: "both",
    });

    console.log("Generated Captcha Text:", captcha.text);

    res.setHeader("Content-Type", "image/png");
    res.send(captcha.imageBuffer);
  } catch (error) {
    console.error("Error generating captcha:", error);
    res.status(500).send("Failed to generate captcha");
  }
});

app.listen(PORT, () => {
  console.log(`Server is running at http://localhost:${PORT}`);
});

NextJS

Route handler:

import { NextResponse } from "next/server";
import { persianCaptchaGenerator } from "persian-captcha-generator";

export async function GET() {
  const captcha = await persianCaptchaGenerator({
    length: 6,
    characterSet: "numbers",
    width: 300,
    height: 100,
    fontSize: 40,
    lineCount: 10,
    dotCount: 100,
    textColor: "#000000",
    backgroundColor: "#f8f9fa",
  });

  const imageBuffer = Buffer.from(captcha.imageBuffer);

  return new NextResponse(imageBuffer, {
    headers: {
      "Content-Type": "image/png",
      "Content-Length": imageBuffer.length.toString(),
    },
  });
}

See full example here

Function API

The persianCaptchaGenerator function accepts the following options: | Parameter | Type | Default | Description | |-----------------|--------------------------------|-----------|------------------------------------------------------------------------------------| | width | number | 200 | Width of the captcha image (in pixels). | | height | number | 80 | Height of the captcha image (in pixels). | | length | number | 5 | Number of characters in the captcha text. | | backgroundColor | string | "#ffffff" | Background color of the captcha image (CSS color value). | | textColor | string | "#000000" | Text color of the captcha characters (CSS color value). | | fontSize | string | 32 | Font size of the captcha characters (in pixels). | | lineCount | string | 5 | Number of random lines drawn over the captcha for obfuscation. | | dotCount | string | 50 | Number of random noise dots added to the captcha image. | | characterSet | numbers, alphabets, both | numbers | Choose the type of characters in the captcha: Persian numbers, alphabets, or both. |

Output

The persianCaptchaGenerator function returns an object with the following properties: | Property | Type | Description | |----------|----------|-------------------------------------------------------| | text | string | The randomly generated captcha text (for validation). | | imageBuffer | Buffer | The PNG image buffer of the generated captcha. |

Sample images

num_white num_green num_yellow num_red alph both

License

MIT

1.4.0

7 months ago

1.3.5

7 months ago

1.3.4

7 months ago

1.3.3

7 months ago

1.3.2

7 months ago

1.3.1

7 months ago

1.3.0

7 months ago

1.2.6

7 months ago

1.2.5

7 months ago

1.2.4

7 months ago

1.2.3

7 months ago

1.2.2

7 months ago

1.2.1

7 months ago

1.2.0

7 months ago

1.1.13

7 months ago

1.1.12

7 months ago

1.1.11

7 months ago

1.1.10

7 months ago

1.1.9

7 months ago

1.1.8

7 months ago

1.1.7

7 months ago

1.1.6

7 months ago

1.1.5

7 months ago

1.1.4

7 months ago

1.1.3

7 months ago

1.1.2

7 months ago

1.1.1

7 months ago

1.1.0

7 months ago

1.0.7

7 months ago

1.0.6

7 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago