PHP code example of vaibhavpandeyvpz / ank

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

    

vaibhavpandeyvpz / ank example snippets




use Ank\CaptchaGenerator;
use Ank\MathCaptchaGenerator;

// Create a text-based CAPTCHA generator
$captcha = new CaptchaGenerator();

// Generate and display the CAPTCHA image
header('Content-Type: image/jpeg');
echo $captcha->getCaptcha();

// Later, validate user input
if ($captcha->isValid($_POST['captcha_code'])) {
    // CAPTCHA is valid
    echo "Verification successful!";
} else {
    // CAPTCHA is invalid
    echo "Invalid CAPTCHA code.";
}



use Ank\MathCaptchaGenerator;

// Create a math-based CAPTCHA generator
$captcha = new MathCaptchaGenerator();

// Generate and display the math problem
header('Content-Type: image/jpeg');
echo $captcha->getCaptcha();

// Validate the answer (user provides numeric answer)
if ($captcha->isValid($_POST['answer'])) {
    echo "Correct answer!";
}



use Ank\CaptchaGenerator;
use Ank\Font;

$captcha = new CaptchaGenerator();

$image = $captcha->getCaptcha()
    ->setBackgroundColor('#000000')      // Black background
    ->setForegroundColor('#FFFFFF')      // White text
    ->setFont(Font::BANGERS)             // Use Bangers font
    ->setSize(300, 100)                  // 300x100 pixels
    ->setQuality(100)                     // Maximum JPEG quality
    ->setDistortion(15, 8);              // More distortion (angle, offset)

header('Content-Type: image/jpeg');
echo $image;



use Ank\CaptchaGenerator;

$captcha = new CaptchaGenerator();
$captcha->setLength(8);  // Generate 8-character codes

$image = $captcha->getCaptcha();



use Ank\CaptchaGenerator;

$customStorage = [];
$captcha = new CaptchaGenerator($customStorage);

// The CAPTCHA answer will be stored in $customStorage
$captcha->getCaptcha('my_captcha_id');



use Ank\CaptchaGenerator;

$captcha = new CaptchaGenerator();

// Generate multiple CAPTCHAs
$image1 = $captcha->getCaptcha('login_form');
$image2 = $captcha->getCaptcha('registration_form');
$image3 = $captcha->getCaptcha('contact_form');

// Validate each independently
if ($captcha->isValid($_POST['login_captcha'], 'login_form')) {
    // Login form CAPTCHA is valid
}

use Ank\Font;

// All available fonts
Font::ACME
Font::BANGERS
Font::BARRIO
Font::BREE_SERIF
Font::FRECKLE_FACE
Font::GOCHI_HAND
Font::LUCKIEST_GUY
Font::PANGOLIN
Font::RALEWAY
Font::RIGHTEOUS
Font::ROBOTO_SLAB
Font::SANSITA

// Get a random font
$randomFont = Font::random();

// Get all fonts
$allFonts = Font::all();

$image->setBackgroundColor('#FFFFFF');  // 6-digit hex with #
$image->setBackgroundColor('FFFFFF');   // 6-digit hex without #
$image->setBackgroundColor('#FFF');     // 3-digit hex with #
$image->setBackgroundColor('FFF');        // 3-digit hex without #



use Ank\CaptchaGenerator;
use Ank\Exception\ImageGenerationException;

try {
    $captcha = new CaptchaGenerator();
    $image = $captcha->getCaptcha();
    echo $image->getImage();
} catch (ImageGenerationException $e) {
    // Handle image generation failure
    error_log('CAPTCHA generation failed: ' . $e->getMessage());
    // Fallback or error page
}


// captcha.php - Generate CAPTCHA image
session_start();

use Ank\CaptchaGenerator;

$captcha = new CaptchaGenerator();
header('Content-Type: image/jpeg');
echo $captcha->getCaptcha('form_captcha');


// form-handler.php - Validate form submission
session_start();

use Ank\CaptchaGenerator;

$captcha = new CaptchaGenerator();

if ($captcha->isValid($_POST['captcha'], 'form_captcha')) {
    // Process form
    echo "Form submitted successfully!";
} else {
    echo "Invalid CAPTCHA. Please try again.";
}


// api/captcha.php
session_start();

use Ank\CaptchaGenerator;

$captcha = new CaptchaGenerator();
$image = $captcha->getCaptcha('ajax_captcha');

header('Content-Type: image/jpeg');
echo $image;