PHPackages                             mmdm/sim-captcha - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mmdm/sim-captcha

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mmdm/sim-captcha
================

A simple yet nice captcha library

v1.0.2(5y ago)116MITPHPPHP &gt;=7.2

Since Aug 18Pushed 5y ago1 watchersCompare

[ Source](https://github.com/mmdm95/sim-captcha)[ Packagist](https://packagist.org/packages/mmdm/sim-captcha)[ RSS](/packages/mmdm-sim-captcha/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependenciesVersions (4)Used By (0)

Simplicity Captcha
==================

[](#simplicity-captcha)

A library for captcha.

Install
-------

[](#install)

**composer**

```
composer require mmdm/sim-captcha
```

Or you can simply download zip file from github and extract it, then put file to your project library and use it like other libraries.

Just add line below to autoload files:

```
require_once 'path_to_library/autoloader.php';
```

and you are good to go.

How to use
----------

[](#how-to-use)

```
// to instance a captcha object
$captcha = CaptchaFactory::instance();

// now you have a captcha object then use it
// like this
$captcha_image = $captcha->generate();

// and put it where you want
// like inside a form

// the output will be like

```

Available functions
-------------------

[](#available-functions)

There are two captcha generator for now.

- The traditional word captcha
- and a simple math captcha that provides \[+, -, \*\]

You can get a new instance from specific class like this

```
// traditional captcha
$captcha = new Captcha(ICaptchaLanguage $language);
// simple math captcha
$captcha = new CaptchaSimpleMath(ICaptchaLanguage $language);
```

The language specified above is to generate a captcha with your locale language words and numbers.

If you want to add a language for your locale you can implement `ICaptchaLanguage` interface. It has three functions you should implement.

```
/**
 * Return numbers of language
 *
 * Note: If there is not any number on that language,
 * return empty array
 *
 * @return array
 */
public function numbers(): array;

/**
 * Return small alpha of language
 *
 * Note: If there is not any small alpha on that language,
 * return empty array
 *
 * @return array
 */
public function alphaSmall(): array;

/**
 * Return capital alpha of language
 *
 * Note: If there is not any capital alpha on that language,
 * return empty array
 *
 * @return array
 */
public function alphaCaps(): array;
```

For convenience there is a `CaptchaFactory` class that creates captcha you want. Just call `instance` method statically.

```
$captcha = CaptchaFactory::instance();
```

If you need to type hint your variable, according to your IDE you should type hint variable. Most of editors should work with `@var`.

```
/**
 * @var Captcha $captcha
 */
$captcha = CaptchaFactory::instance();

// now you can access to Captcha class, methods
$captcha->...;
```

`instance(int $type = CaptchaFactory::CAPTCHA, ICaptchaLanguage $language = null)`

As you can see there is a type variable that you can specify which class you need to instantiate. Under `CaptchaFactory` constants there are `CaptchaFactory::CAPTCHA` and `CaptchaFactory::CAPTCHA_SIMPLE_MATH`for class instantiating.

Second parameter is language of captcha. If you don't specify that, it'll be English by default.

Note: Please use constants, because if numbers changed in future, you will have problem and have to refactoring captcha instantiating.

```
$captcha = CaptchaFactory::instance(CaptchaFactory::CAPTCHA, new Persian());
$captcha->setFont(CaptchaFactory::FONT_IRAN_SANS);
```

Note: For now there are three languages: \[English, Persian, Arabic\].

\*\*\* **Important Note** \*\*\* Generate captcha codes in any languages are from **Left**to **Right**. Please inform user from this behavior if you have rtl language alphabet.

#### Common Methods

[](#common-methods)

`generate()`

This method will generate captcha

```
$generated_captcha = $captcha->generate();
```

`verify($input)`

This method is to verify generated captcha by sending user's value.

```
$is_ok = $captcha->verify($user_input);
```

`setName(string $name = null)`

With this method you can store captcha under a name you specify. For example if you want to have captcha in several pages and user want to use multiple pages that have captcha, those generated captcha will mixed up and just one page can submit and others will say captcha is not valid! To solve this problem you should specify a name for generated captcha to prevent that.

Default name is captcha

```
$captcha->setName('contact_us');
```

`getName()`

This method returns captcha name.

```
$name = $captcha->getName();
```

`setExpiration(int $expire_time)`

You can specify expiration of captcha with this method

Default expiration is 600s

Note: pass time as seconds

```
// set expiration to 2min or 120s
$captcha->setExpiration(120);
```

`getExpiration()`

This method returns expiration time of captcha in seconds

```
$exp_time = $captcha->getExpiration();
```

#### Common Methods in Text Captcha

[](#common-methods-in-text-captcha)

It has all methods of `Common Methods` and below methods.

`generateBase64Code(string $code = null)`

This method returns base64 image code only.

```
$base64_code = $captcha->generateBase64Code();
// or
$base64_code = $captcha->generateBase64Code('mmdm95');
```

`setWidth(int $width)`

This method sets the captcha image width

Default is *200*.

```
$captcha->setWidth(300);
```

`getWidth(): int`

This method returns captcha image width.

```
$width = $captcha->getWidth();
```

`setHeight(int $height)`

This method sets the captcha image height

Default is *50*.

```
$captcha->setHeight(70);
```

`getHeight(): int`

This method returns captcha image height.

```
$height = $captcha->getHeight();
```

`setFont(string $filename)`

You can specify the font of captcha. Just send font's filename as parameter. By default there are three fonts with this library:

- English -&gt; Menlo-Regular,
- Persian -&gt; IRANSansWeb,
- Arabic -&gt; Lateef-Regular,

These fonts' filename have constant under `CaptchaFactory` class: `CaptchaFactory::FONT_MENLO`, `CaptchaFactory::FONT_LATEEF` and `CaptchaFactory::FONT_IRAN_SANS`

or if you have other fonts to use, just send filename as parameter.

```
$captcha->setFont($path_to_your_font_filename);
```

`getFont()`

This method returns font's filename string

```
$font_filename = $captcha->getFont();
```

`setFontSize(int $size)`

You can specify size of captcha's font.

Default is *20*.

```
$captcha->setFontSize(25);
```

`getFontSize(): int`

This method returns captcha's font size

```
$font_size = $captcha->getFontSize();
```

`setImgAttributes(array $attributes)`

Because of generate method result, that is an image(for captcha text), setting attributes is a problem. With this method you can set attribute of captcha image.

```
$captcha->setImgAttributes([
    'style' => 'display: block',
    'class' => 'img-rounded',
    'id' => 'captchaImage',
    'alt' => 'captcha image',
    ...
]);
```

`getImgAttributes(): array`

This method returns captcha image attributes as array

```
$attributes = $captcha->getImgAttributes();

// output will be something like this
[
    'style' => 'display: block',
    'class' => 'img-rounded',
    'id' => 'captchaImage',
    'alt' => 'captcha image',
    ...
]
```

`getImgAttributesString(): string`

This method returns captcha image attributes as string

```
$attributes_string = $captcha->getImgAttributesString();

// output will be something like this
style="display: block" class="img-rounded" id="captchaImage" alt="captcha image" ...
```

`addNoise(bool $answer)`

If you need to have noise on captcha, send true as parameter to this method.

Default is *true*

```
$captcha->addNoise(false);
```

`useEnglishNumbersToVerify(bool $answer)`

If your user enter the captcha characters as input, it may be valid but not valid! It is because the user enter numbers in another language than locale. Like arabic numbers that user enter them as english, they may be equal but different in the same time. You can prevent that with sending *true* as parameter to this method and it'll convert all numbers from specified language to english and then verify entered code.

Default is *false*

Note: Use this method before verify method.

Note: Be careful and specify language before verify method to prevent unwanted behaviors.

```
$captcha->useEnglishNumbersToVerify(true);
```

#### Captcha

[](#captcha)

It has all methods of `Common Methods in Text Captcha` and below methods.

`generate(string $code = null)`

This method generates a captcha code. Also you can set your own code to generate as captcha.

```
$captcha_image = $captcha->generate();
// or
$captcha_image = $captcha->generate('mmdm95');
```

`setLength(int $length): Captcha`

With this method you can set length of generated captcha

Default is *6*.

```
$captcha->setLength(8);
```

`getLength(): int`

Get length of captcha characters that used in code generation

```
$length = $caotcha->getLength();
```

`setDifficulty(int $difficulty): Captcha`

There is three difficulty for captcha codes \[easy, medium, hard\] that can pass to this method using constants under `CaptchaFactory`:

1. CaptchaFactory::DIFFICULTY\_EASY
2. CaptchaFactory::DIFFICULTY\_NORMAL
3. CaptchaFactory::DIFFICULTY\_HARD

Easy makes captcha code to be just numbers.

Normal makes captcha code to be numbers and capital characters.

Hard makes captcha code to be numbers, capital characters and small characters.

Default is `CaptchaFactory::DIFFICULTY_NORMAL`

Note: All numbers and alphabet used from specified language

Note: Because `CaptchaFactory::DIFFICULTY_NORMAL` use capital characters, it is CASE-INSENSITIVE.

```
$captcha->setDifficulty(CaptchaFactory::DIFFICULTY_HARD);
```

`getDifficulty(): int`

Get captcha difficulty

```
$difficulty = $captcha->getDifficulty();
```

#### CaptchaSimpleMath

[](#captchasimplemath)

It has all methods of `Common Methods in Text Captcha` and below methods.

`setNumbersCount(int $count): CaptchaSimpleMath`

You can specify how many numbers should participate in. It should be a number between 2 and 5(inclusive).

Default is *2*.

```
$captcha->setNumbersCount(3);
```

`getNumbersCount(): int`

This method returns numbers of participated numbers in captcha.

```
$count = $captcha->getNumbersCount();
```

`useMultiplyOperands(): CaptchaSimpleMath`

By default the operands for math are \[+, -\] and if you need multiplication, just call this method before captcha generation.

```
$captcha->useMultiplyOperands();
```

How to generate captcha multiple times in a page
================================================

[](#how-to-generate-captcha-multiple-times-in-a-page)

Because of page caching, you can not generate captcha with the captcha object in one request, therefore you should use ajax.

In tests folder you can see an example of this usage. The captcha will generate by ajax from beginning.

**index.php** is the base page for it and **captcha.php** is the page that gives captcha each time.

See **script** part of **index.php** if you are not familiar with ajax creation or not using a library like **jQuery** to create ajax request.

Note: If you store captcha under a specific name, be careful and send name of captcha with ajax request.

License
=======

[](#license)

Under MIT license.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~54 days

Total

3

Last Release

1990d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d955ebaae3fe602e6ad26c16422f6ab25a61a436fd7f08425a5be5c73ef9025c?d=identicon)[mmdm95](/maintainers/mmdm95)

---

Top Contributors

[![mmdm95](https://avatars.githubusercontent.com/u/26489185?v=4)](https://github.com/mmdm95 "mmdm95 (16 commits)")

### Embed Badge

![Health badge](/badges/mmdm-sim-captcha/health.svg)

```
[![Health](https://phpackages.com/badges/mmdm-sim-captcha/health.svg)](https://phpackages.com/packages/mmdm-sim-captcha)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
