2017 © Pedro Peláez
 

library captcha-generator

A PHP library for generating Captcha challenges using libgd.

image

dkh/captcha-generator

A PHP library for generating Captcha challenges using libgd.

  • Wednesday, July 11, 2018
  • by dangkyokhoang
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

PHP Captcha Generator

Description

A PHP library for generating Captcha challenges using libgd., (*1)

Captcha examples

  • Easy expression captcha:
    PHP Captcha Generator - Expression - Easy
  • Hard expression captcha:
    PHP Captcha Generator - Expression - Hard
  • Easy string captcha:
    PHP Catpcha Generator - String - Easy
  • Hard string captcha (colored):
    PHP Captcha Generator - String - Hard

Repositories

  • GitHub: https://github.com/dangkyokhoang/PHP-Captcha-Generator.
  • Packagist: https://packagist.org/packages/dkh/captcha-generator.

Required dependencies

  • GD Graphics Library (ext-gd).

User Guide

Installation

You can easily get the library installed on your project by running this command. composer require dkh/captcha-generator, (*2)

Implementation

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

To create a captcha, use new *Captcha($size?, $level?). ```php // Default size: 3 and default difficulty level: 1 $expression_captcha = new ExpressionCaptcha(); $string_captcha = new StringCaptcha();, (*3)

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

Get captcha's 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. ```php $_SESSION['captcha'] = $captcha->solve();, (*5)

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

Verify user's captcha input

To verify user's captcha input, compare it with the captcha's previously solved value stored somewhere. php $user_captcha_input = $_POST['captcha'] ?? ''; $is_matched = $_SESSION['captcha'] === $user_captcha_input;, (*7)

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. ```php $base64_image = $captcha->render(); echo sprintf('', $base64_image);, (*8)

// 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);, (*9)

// 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 ); ```, (*10)

Example implementation of the library

```php <?php, (*11)

require_once 'vendor/autoload.php';, (*12)

use Dkh\ExpressionCaptcha;, (*13)

session_start();, (*14)

// Verify user's captcha input if (isset($_POST['captcha']) && isset($_SESSION['captcha'])) { $is_matched = $_POST['captcha'] === $_SESSION['captcha']; } else { $is_matched = null; } $message = $is_matched !== null ? ($is_matched ? 'Captcha matched' : 'Captcha not matched') : 'Try solving the captcha';, (*15)

// Create a captcha $captcha = new ExpressionCaptcha(); // Store captcha's solved value $_SESSION['captcha'] = $captcha->solve(); // Render the captcha into image // The return value is a PNG image string encoded with base64 $base64_image = $captcha->render();, (*16)

echo sprintf( '' . '' . ', (*17)

' . '
' . '
' . '' . '
' . '

' . ', (*18)

Message: %s

' . '' . '', $base64_image, $message ); ```, (*19)

The Versions

11/07 2018

dev-master

9999999-dev https://github.com/dangkyokhoang/PHP-Captcha-Generator

A PHP library for generating Captcha challenges using libgd.

  Sources   Download

MIT

The Requires

  • php >=7.2.0
  • ext-gd *

 

The Development Requires

php generator captcha dkh

11/07 2018

1.1.0

1.1.0.0 https://github.com/dangkyokhoang/PHP-Captcha-Generator

A PHP library for generating Captcha challenges using libgd.

  Sources   Download

MIT

The Requires

  • php >=7.2.0
  • ext-gd *

 

The Development Requires

php generator captcha dkh

11/07 2018

1.0.0

1.0.0.0 https://github.com/dangkyokhoang/PHP-Captcha-Generator

A PHP library for generating Captcha challenges using libgd.

  Sources   Download

MIT

The Requires

  • php >=7.2.0

 

The Development Requires

php generator captcha dkh