PHP code example of hpd / captcha
1. Go to this page and download the library: Download hpd/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/ */
hpd / captcha example snippets
// 1. Default noise based on difficulty
$this->addNoise(200, 100);
// 2. Only change mode
$this->addNoise(200, 100, ['mode' => 'cross']);
// 3. Override xRange and yRange manually
$this->addNoise(200, 100, [
'xRange' => [2, 6],
'yRange' => [3, 7],
'mode' => 'horizontal',
'density' => 0.4,//or $this->noiseDensity
'intensity'=> 0.7 //or $this->noiseIndensity
]);
return [
'disable' => env('CAPTCHA_DISABLE', env('APP_ENV') !== 'production'),
'characters' => [
'lowercase' => 'abdefghjklmnpqrstuvwxyz',
'uppercase' => 'ABCDEFGHJKLMNPQRSTUVWXYZ',
'digits' => '23456789' //
],
'default' => [
'length' => 5,
'bgColor' => '#FFFFFF',
'color' => 'multi',
'noiseLines' => true,
'noise' => true,
'noiseColor' => '#FFFFFF',
'sensitive' => false,
'digits' => true,
'uppercase' => true,
'lowercase' => true,
'alpha' => 10,
'blur' => true,
],
...
];
protected $config;
protected string $bgColor = "#000000";//'random'
protected string $color = "#FFFFFF"; //'random','multi'
protected string $noiseColor = "#FFFFFF";//'random'
protected bool $blur = false;
protected int $alpha = 0; // 0 -> 1
protected bool $noise = false;
protected string $noiseMode = 'cross';//'vertical','horizontal','cross','random'
protected float $noiseDensity = 0.1; // 0 -> 1.0
protected float $noiseIntensity = 1.0; // 0 -> 1.0
protected bool $noiseLines = false;
protected bool $difficulty = false;
protected int $length = 5;
protected int $expire = 60;
protected bool $sensitive = false;
protected array $characters;
protected bool $lowercase = true;
protected bool $uppercase = false;
protected bool $digits = false;
protected string $fontDir = '';
protected string $font = 'libre.ttf';
protected int $fontSize = 24;
protected string $type = 'string';
captcha()// returns image
captcha_get_src()// returns image source(URl)
captcha_get_img()// returns img html element
captcha('default')// returns image
captcha_get_src('easy')// returns image source(URl)
captcha_get_img('dark')// returns img element
<img src="{{captcha_get_src()}}" title="Captcha" alt="Captcha">
<div> {!!captcha_get_img()!!}</div>
Route::post('captcha_check', function() {
$validator = Validator::make($request->all(), [
'captcha' => 'alidator)
->withInput();
}
});
[
'token'=>'816fc4e459eb3bd240a58beee41e723df2d1b1f29300f2a7920cdc372f728695'
,
'image'=>'data:image/png;base64,'.$this->createBase64FromImg($this->image)
]
$validator = validator()->make(request()->all(),
['captcha' => 'ponse()->json([
'captcha' => 'Invalid captcha',
],422);
} else {
// continue
}
bash
php artisan vendor:publish --tag=captcha-config