PHP code example of crutch / captcha-generator

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

    

crutch / captcha-generator example snippets




use Crutch\CaptchaGenerator\CaptchaGenerator;

(250, 100) // Image size. By default, 160x80
    ->withBackgroundColor(20, 20, 20) // Set background color (r, g, b). By default, random light color
    ->withForegroundColor(200, 200, 200) // Set foreground color (r, g, b). By default, random dark color
    ->withCredits('crutch captcha') // Added text to image bottom. By default, NULL
    ->withSpaces(true) // Adds spaces between characters. By default, FALSE
    ->withFluctuationAmplitude(0) // Vertical fluctuation amplitude. By default, 8
    ->withWhiteNoiseDensity(.1) // White noise density. By default, 1 / 6
    ->withBlackNoiseDensity(.05) // White noise density. By default, 1 / 30
    ->asJpeg(90) // Use JPEG format with quality from 1 to 100
    ->asWebp(80) // Use WEBP format with quality from 1 to 100
    ->asGif() // Use GIF format
    ->asPng(8) // Use PNG format with quality from 1 to 9
;

//
$symbols = '23456789abcdegkpqsvxyz'; // alphabet without similar symbols (o=0, 1=l, i=j, t=f)

$text = '';
for ($i = 0; $i < 6; $i++) {
    $text .= substr($symbols, mt_rand(0, strlen($symbols) - 1), 1);
}

$image = $generator->generate($text); // generate PNG image

file_put_contents('/tmp/captcha.png', $image); // save to file

echo sprintf('<img src="data:image/png;base64,%s" alt="captcha"/>', base64_encode($image)); // out inline



use Crutch\CaptchaGenerator\CaptchaGenerator;

('/path/to/output-font-simple.png', true) // add custom font and unset others
    ->withFont('/path/to/output-font-outline.png') // add other custom font
;

$image = $generator->generate('abc'); // generate PNG image