PHP code example of mmdm / sim-captcha

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

    

mmdm / sim-captcha example snippets

 
composer 



// 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
<img src="BASE 64 ENCODED STRING">

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

/**
 * 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;

$captcha = CaptchaFactory::instance();

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

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

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

$generated_captcha = $captcha->generate();

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

$captcha->setName('contact_us');

$name = $captcha->getName();

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

$exp_time = $captcha->getExpiration();

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

$captcha->setWidth(300);

$width = $captcha->getWidth();

$captcha->setHeight(70);

$height = $captcha->getHeight();

$captcha->setFont($path_to_your_font_filename);

$font_filename = $captcha->getFont();

$captcha->setFontSize(25);

$font_size = $captcha->getFontSize();

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

$attributes = $captcha->getImgAttributes();

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

$attributes_string = $captcha->getImgAttributesString();

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

$captcha->addNoise(false);

$captcha->useEnglishNumbersToVerify(true);

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

$captcha->setLength(8);

$length = $caotcha->getLength();

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

$difficulty = $captcha->getDifficulty();

$captcha->setNumbersCount(3);

$count = $captcha->getNumbersCount();

$captcha->useMultiplyOperands();