PHPackages                             dkh/captcha-generator - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. dkh/captcha-generator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

dkh/captcha-generator
=====================

A PHP library for generating Captcha challenges using libgd.

1.1.0(7y ago)16.7kMITPHPPHP &gt;=7.2.0

Since Jul 11Pushed 7y ago1 watchersCompare

[ Source](https://github.com/dangkyokhoang/php-captcha-generator)[ Packagist](https://packagist.org/packages/dkh/captcha-generator)[ Docs](https://github.com/dangkyokhoang/PHP-Captcha-Generator)[ RSS](/packages/dkh-captcha-generator/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

PHP Captcha Generator
=====================

[](#php-captcha-generator)

### Description

[](#description)

A PHP library for generating Captcha challenges using libgd.

### Captcha examples

[](#captcha-examples)

- Easy expression captcha:
    [![PHP Captcha Generator - Expression - Easy](https://camo.githubusercontent.com/c137d9aec1cc4f12e904297de245317193eaab4091198434c03078fc28ba6d77/68747470733a2f2f696d6167652e6962622e636f2f6253576845382f696e6465782e706e67)](https://camo.githubusercontent.com/c137d9aec1cc4f12e904297de245317193eaab4091198434c03078fc28ba6d77/68747470733a2f2f696d6167652e6962622e636f2f6253576845382f696e6465782e706e67)
- Hard expression captcha:
    [![PHP Captcha Generator - Expression - Hard](https://camo.githubusercontent.com/4c6ea2fd3e40567bef6f14f4bb52770e12eaf70288c938bd530c35666500e48f/68747470733a2f2f707265766965772e6962622e636f2f63656a7075382f696e6465782e706e67)](https://camo.githubusercontent.com/4c6ea2fd3e40567bef6f14f4bb52770e12eaf70288c938bd530c35666500e48f/68747470733a2f2f707265766965772e6962622e636f2f63656a7075382f696e6465782e706e67)
- Easy string captcha:
    [![PHP Catpcha Generator - String - Easy](https://camo.githubusercontent.com/a6676e5d3863b92ede653e535b5a0144620435ffcb0f503fa69f63a6f689ffd7/68747470733a2f2f696d6167652e6962622e636f2f6d7158466a382f696e6465782e706e67)](https://camo.githubusercontent.com/a6676e5d3863b92ede653e535b5a0144620435ffcb0f503fa69f63a6f689ffd7/68747470733a2f2f696d6167652e6962622e636f2f6d7158466a382f696e6465782e706e67)
- Hard string captcha (colored):
    [![PHP Captcha Generator - String - Hard](https://camo.githubusercontent.com/14713b0d7d27dfe46e4692dfbab3acfb4839c2f8c0bee12f618d3abe7b71b993/68747470733a2f2f696d6167652e6962622e636f2f6554563150382f696e6465782e706e67)](https://camo.githubusercontent.com/14713b0d7d27dfe46e4692dfbab3acfb4839c2f8c0bee12f618d3abe7b71b993/68747470733a2f2f696d6167652e6962622e636f2f6554563150382f696e6465782e706e67)

### Repositories

[](#repositories)

- GitHub: .
- Packagist: .

### Required dependencies

[](#required-dependencies)

- GD Graphics Library (ext-gd).

User Guide
==========

[](#user-guide)

Installation
------------

[](#installation)

You can easily get the library installed on your project by running this command.

```
composer require dkh/captcha-generator

```

Implementation
--------------

[](#implementation)

### Captcha types

[](#captcha-types)

- `ExpressionCaptcha` expression captcha requires users to do basic arithmetic operations (addition, subtraction, multiplication and division) to solve.
- `StringCaptcha` string captcha only requires users to recognize the characters in the string.

### Create a captcha

[](#create-a-captcha)

To create a captcha, use `new *Captcha($size?, $level?)`.

```
// Default size: 3 and default difficulty level: 1
$expression_captcha = new ExpressionCaptcha();
$string_captcha = new StringCaptcha();

// Specific size and difficulty level
$size = 10;
$level = 2;
$another_expression_captcha = new ExpressionCaptcha($size, $level);
```

### Get captcha's solved value

[](#get-captchas-solved-value)

To get captcha's solved value, call `$captcha->solve()` or `*Captcha::solveString($string)`.
Store the solved value somewhere, e.g in a session variable, to later verify user's captcha input.

```
$_SESSION['captcha'] = $captcha->solve();

// Or in a way that is infrequent,
// use static method *Captcha::solveString()
$my_expression = '1+6:3-2*4';
$_SESSION['my_captcha'] = ExpressionCaptcha::solveString($my_expression);
```

### Verify user's captcha input

[](#verify-users-captcha-input)

To verify user's captcha input, compare it with the captcha's previously solved value stored somewhere.

```
$user_captcha_input = $_POST['captcha'] ?? '';
$is_matched = $_SESSION['captcha'] === $user_captcha_input;
```

### Display the captcha image

[](#display-the-captcha-image)

To render captcha into image, call `$captcha->render($options?)`, `Captcha::renderString($string, $options?)`. Return value is a PNG image data string encoded with base64.
To dislay the captcha image, use data URL `data:image/png;base64`, or save the rendered image somewhere and return the image's path.

```
$base64_image = $captcha->render();
echo sprintf('', $base64_image);

// Or in a way like this:
$my_string = 'any will do?';
$image_path = 'captcha.png';
$base64_image_to_be_saved = Captcha::renderString($my_string);
file_put_contents(
    $image_path,
    base64_decode($base64_image_to_be_saved)
);
echo sprintf('', $image_path);

// Image rendered with some options
$another_base64_image = $captcha->render([
    'height' => 50,
    'fill' => [0, 0, 0, 30],
    // The alpha channel is optional
    'color' => [255, 255, 255]
]);
echo sprintf(
    '',
    $another_base64_image
);
```

### Example implementation of the library

[](#example-implementation-of-the-library)

```
