PHP code example of eusonlito / captcha

1. Go to this page and download the library: Download eusonlito/captcha library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eusonlito / captcha example snippets


 use Eusonlito\Captcha\Captcha; 


use Eusonlito\Captcha\Captcha;

function validate()
{
    if (!Captcha::check()) {
        throw new Exception('Captcha text is not correct');
    }
}


# config/app.php

return [
    ...

    'aliases' => [
        ...

        'Captcha' => 'Eusonlito\Captcha\Captcha',

        ...
    ]
];


use Eusonlito\Captcha\Captcha;

# Simple usage with fixed word length
Captcha::source($LETTERS_COUNT, $WIDTH, $HEIGHT); # Print base64 source image code

# Define min and max word length
Captcha::source(array($LETTERS_MIN, $LETTERS_MAX), $WIDTH, $HEIGHT); # Print base64 source image code

# Same using img tag
Captcha::img($LETTERS_COUNT, $WIDTH, $HEIGHT); # Print img tag
Captcha::img(array($LETTERS_MIN, $LETTERS_MAX), $WIDTH, $HEIGHT); # Print img tag

# Img tag with parameters
Captcha::img($LETTERS_COUNT, $WIDTH, $HEIGHT, array('class' => 'img-responsive')); # Print img tag with class attribute

# Simple input tag print
Captcha::input(); # Print input tag

# Input tag with parameters
Captcha::input(array('class' => 'form-control')); # Print input tag with class attribute


use Eusonlito\Captcha\Captcha;

# Define a unique font to use (only .ttf)
Captcha::setFont(__DIR__.'/../fonts/couture-bold.ttf'); # string or array

# Add fonts to repository (only .ttf)
Captcha::addFont(array(
    __DIR__.'/../fonts/couture-bold.ttf',
    __DIR__.'/../fonts/brush-lettering-one.ttf'
));

# Set custom rgb background. Default is 255, 255, 255
Captcha::setBackground([120, 120, 120]);

# Set custom hex background.
Captcha::setBackground('#FFF000');

# Set transparent background.
Captcha::setBackground('transparent');

# Set custom rgb font color. Default is 115, 115, 115
Captcha::setColor([50, 50, 50]);

# Set custom hex color.
Captcha::setColor('#000FFF');

# Set custom padding to captcha image (approximate). Default is 0.4
Captcha::setPadding(20); // Fixed value in pixels
Captcha::setPadding(0.4); // Percent value

# Set image noise. Default is without noise
Captcha::setNoise($POINTS, $LINES); // Fixed points and lines noise
Captcha::setNoise(array($POINTS_MIN, $POINTS_MAX), array($LINES_MIN, $LINES_MAX)); // Variable points and lines noise
Captcha::setNoise(null, array($LINES_MIN, $LINES_MAX)); // Avoid points noise
Captcha::setNoise(array($POINTS_MIN, $POINTS_MAX), null); // Avoid lines noise

# Set custom available letters. Default are 'ABCDEFGHJKLMNPRSTUVWXYZ'
Captcha::setLetters('ABCDE3456');

# Set custom session name captcha storage (captcha string is stored crypted). Default is 'captcha-string'
Captcha::sessionName('my-captcha');

# Enable session before use on non session environments
Captcha::sessionStart();