PHP code example of mobicms / captcha

1. Go to this page and download the library: Download mobicms/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/ */

    

mobicms / captcha example snippets



$captcha = new Mobicms\Captcha\Image();
$_SESSION['code'] = $captcha->getCode();

if (empty($_POST['code']) || empty($_SESSION['code'])) {
    // If you do not enter a verification code,
    // or there is no session variable
} else {
    // We use a comparison that is independent of the Letter Case
    if (strtolower(trim($_POST['code'])) === strtolower($_SESSION['code'])) {
        // If your code passes validation
    } else {
        // If the code verification fails
    }
}

//////////////////////////////////////////////////////////
// THIS IS IMPORTANT!                                   //
// Regardless of the result of the check, in any case,  //
// the session variable must be deleted!!!              //
//////////////////////////////////////////////////////////
unset($_SESSION['code']);

$captcha = new Mobicms\Captcha\Image();

$captcha->imageWidth = 250;
$captcha->imageHeight = 100;

$captcha = new Mobicms\Captcha\Image();

$captcha->fontFolders = array_merge(
    // Using built-in fonts 
    $captcha->fontFolders,

    // Specifying your font folders
    ['folder1', 'folder2']
);

$captcha = new Mobicms\Captcha\Image();

$adjust = [
    // Specify the name of the font file
    'myfont1.ttf' => [
        // Specify the relative font size.
        // It will be summarized with the default size specified in the $defaultFontSize property
        // In this case, the font will be used: 30+16=46
        'size' => 16, // Optional
        // Forcing the use of only lowercase characters of the specified font
        'case' => \Mobicms\Captcha\Image::FONT_CASE_LOWER, // Optional
    ],

    'myfont2.ttf' => [
        // Forcing the use of only uppercase characters of the specified font
        'case' => \Mobicms\Captcha\Image::FONT_CASE_UPPER,
    ],

    'myfont3.ttf' => [
        // Font size will be decreased by 8
        'size' => -8,
    ],

    'myfont4.ttf' => [
        // Font size will be increased by 4
        'size' => 4,
    ],
];

$captcha->fontsTune = array_merge($captcha->fontsTune, $adjust);

$code = 'FooBar';
$captcha = new Mobicms\Captcha\Image($code);