npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

persian-captcha-generator

v1.3.0

Published

A library for generating customizable captchas with Persian numbers and alphabets using node-canvas.

Downloads

1,402

Readme

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_red num_yellow alph both

License

MIT