Laravel Captcha Generator
- PHP 5.6+
- Laravel Framework 5.2+
- GD Library enabled
- Run
composer require zerosdev/laravel-captcha:dev-master
- Add this code to your config/app.php in providers array
ZerosDev\LaravelCaptcha\ServiceProvider::class,
- Add this code to your config/app.php in aliases array
'Captcha' => ZerosDev\LaravelCaptcha\Facade::class,
- Run
composer dump-autoload
- Run
php artisan vendor:publish --provider="ZerosDev\LaravelCaptcha\ServiceProvider"
Open your config/captcha.php and adjust the setting. Optionally, you can adjust some captcha setting on-the-fly with available methods below:
Set the characters that will be used as captcha text
Set the captcha image width and height in pixel (px)
Set the captcha character length
// Import from root class (Captcha Facade)
use Captcha;
// generate captcha
$captcha = Captcha::chars('123456789ABCDEFGHIJKLMNPQRSTUVWXYZ')->length(4)->size(120, 40)->generate();
// get captcha id
$id = $captcha->id();
// return random generation id
// print html hidden form field, used in blade template
{{ $captcha->form_field() }}
// return: <input type="hidden" name="captcha_id" value="XXXXXXXXXXXXXX" id="captcha_id">
// get base64 image
$image = $captcha->image();
// return data:image/png; base64,XXXXXXXXXXXXXX
// print html image, used in blade template
{{ $captcha->html_image(['onclick' => 'jsFunction()', 'style' => 'border:1px solid #ddd']) }}
// return: <img src="data:image/png; base64,XXXXXXXXXXXXXX" onclick="jsFunction()" style="border:1px solid #ddd" />
// Import from root class (Captcha Facade)
use Captcha;
// validate captcha
// $captchaId = Captcha generation id, $captcha->id()
// $captchaText = Captcha input from client request
if( Captcha::validate($captchaId, $captchaText) )
{
// Valid
}
else
{
// Invalid
}
It is recommended to avoid using the "0" (zero) and "O" characters in captcha to avoid being ambiguous