PHP code example of wackowiki / freecap

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

    

wackowiki / freecap example snippets


use FreeCap\FreeCap;
use FreeCap\SessionAdapter;

// 1. Initialize the adapter with your session mechanism
//    SessionAdapter accepts native arrays or objects with magic properties (like WackoWiki's)
$adapter = new SessionAdapter($_SESSION);

// 2. Optional: Pass custom configuration
$config = [
    'use_dict' => 1,
    'bg_type'  => 1,
    'output'   => 'webp',
];

// 3. Generate and render
$captcha = new FreeCap($adapter, $config);
$captcha->render(); // Exits script after sending image

use FreeCap\FreeCap;
use FreeCap\SessionAdapter;

// When the form page is loaded, flag that the CAPTCHA is active:
// $adapter = new SessionAdapter($_SESSION);
// $adapter->set('freecap_shown', 1);

// On form submission:
$adapter = new SessionAdapter($_SESSION);
$captcha = new FreeCap($adapter);

$input = $_POST['captcha'] ?? '';

if ($captcha->validate($input)) {
    echo "CAPTCHA passed!";
} else {
    echo "CAPTCHA failed. Please try again.";
}

$config = [
    'asset_path'       => '/path/to/resources',  // Defaults to ../resources
    'site_tags'        => null,                   // Array of strings to display on the image
    'tag_pos'          => 1,                      // 0: top, 1: bottom, 2: both
    'algo'             => 'sha1',                 // 'sha1', 'sha256', 'sha512'
    'output'           => 'webp',                 // 'avif', 'gif', 'jpg', 'png', 'webp'
    'use_dict'         => 1,                      // 0: random string, 1: dictionary file
    'max_word_length'  => 6,                      // Maximum characters in the CAPTCHA
    'col_type'         => 1,                      // 0: one color, 1: different color per letter
    'max_attempts'     => 15,                    // Max refresh attempts before lockout
    'bg_type'          => 1,                      // 0: transparent, 1: grid, 2: squiggles, 3: morphed blocks
    'blur_bg'          => true,                   // Blur the background?
    'morph_bg'         => true,                   // Morph the background?
    'merge_type'       => 0,                      // 0: merge CAPTCHA with bg, 1: write over bg
];

$captcha = new FreeCap($adapter, $config);

use FreeCap\AdapterInterface;

class MyCacheAdapter implements AdapterInterface
{
    private Cache $cache;

    public function __construct(Cache $cache)
    {
        $this->cache = $cache;
    }

    public function get(string $key, mixed $default = null): mixed
    {
        return $this->cache->get('captcha_' . $key, $default);
    }

    public function set(string $key, mixed $value): void
    {
        $this->cache->set('captcha_' . $key, $value);
    }

    public function delete(string $key): void
    {
        $this->cache->delete('captcha_' . $key);
    }
}
text
freecap/
├── resources/
│   ├── .ht_freecap_font1.gdf
│   ├── .ht_freecap_font2.gdf
│   ├── .ht_freecap_font3.gdf
│   ├── .ht_freecap_font4.gdf
│   ├── .ht_freecap_font5.gdf
│   ├── .ht_freecap_im1.jpg
│   ├── ...
│   └── .ht_freecap_words
├── src/
│   ├── AdapterInterface.php
│   ├── FreeCap.php
│   └── SessionAdapter.php
├── tests/
├── composer.json
└── LICENSE