Image Processing PHP Extension (GD Image or Imagick)
Installing Captcha Package
To integrate the Intervention Image library for captcha generation, execute the following command:
1
composer require intervention/image
Downloading Captcha Font
For our captcha text, we’ll utilize the Open Sans font. Download the font from Google Fonts
and place it in the resources/fonts directory of your Laravel project.
Captcha Generation
Let’s delve into the implementation of captcha generation logic. In this section of the code, we generate a random captcha consisting of a four-digit number. We then use the Intervention Image library to create a captcha image with the generated number. The image is rendered with a white background and the Open Sans Medium Italic font. The font size is randomized between 17 and 20 pixels, and the captcha text is centered both horizontally and vertically within the image. Finally, the captcha value is stored in the session for validation purposes.
// File: app/Http/Controllers/CaptchaController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
classCaptchaControllerextends Controller
{
/**
* Generate and return a captcha image.
*
* @return \Illuminate\Http\Response
*/publicfunctiongetCaptcha()
{
// Generate a random captcha (four-digit number)
$captcha= rand(1000, 9999);
// Create an instance of ImageManager
$manager=new ImageManager(new Driver());
// Create a canvas for the captcha image
$image=$manager->create(120, 40);
// Add the captcha text to the image
$image->text($captcha, 60, 20, function($font) {
// Set the font file and size
$font->file(resource_path('fonts/OpenSans-MediumItalic.ttf'));
$font->size(rand(17, 20));
// Center the text horizontally and vertically
$font->align('center');
$font->valign('middle');
});
// Encode the image as a data URL
$data=$image->encodeByMediaType('image/png')->toDataUri();
// Store the captcha value in the session
Session::put('captcha', $captcha);
// Return the captcha image as JSON response
return response()->json([
'captcha'=>$data,
]);
}
}
Captcha Validation
Now, let’s explore the implementation of captcha validation logic. In this part of the code, we validate the user-submitted captcha input. First, we ensure that the captcha input is present and contains at least four characters. Then, we retrieve the stored captcha value from the session. If the stored captcha value is not found or if it does not match the user-submitted captcha input, we return a JSON response indicating that the captcha validation has failed. Otherwise, we return a JSON response indicating that the captcha validation was successful.
// File: app/Http/Controllers/CaptchaController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
classCaptchaControllerextends Controller
{
/**
* Validate the captcha input.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/publicfunctionvalidateCaptcha(Request $request)
{
// Validate the captcha input
$request->validate([
'captcha'=>'required|min:4', // Captcha must be 4 characters long.
]);
// Retrieve the stored captcha value from the session
$captcha= Session::get('captcha');
// Check if the captcha value is missing or does not match the user-submitted captcha input
if (!$captcha||$request->captcha!=$captcha) {
// Return a JSON response indicating that the captcha validation failed
return response()->json([
'message'=>'The captcha value is invalid. Please try again.',
], 422);
}
// Captcha validation passed.
return response()->json([
'message'=>'Captcha validation successful!',
]);
}
}
Captcha Routes
1
2
3
4
5
6
7
8
9
// File: routes/web.php
use App\Http\Controllers\CaptchaController;
// Route to handle captcha generation
Route::get('/captcha', [CaptchaController::class, 'getCaptcha'])->name('captcha');
// Route to handle captcha validation
Route::post('/validate-captcha', [CaptchaController::class, 'validateCaptcha'])->name('validate.captcha');
Demo Route
To showcase our captcha functionality, let’s create a demo route:
1
2
3
4
5
// File: routes/web.php
Route::get('/demo', function () {
return view('captcha_demo');
});
Now, let’s create a Blade view named captcha_demo.blade.php in the resources/views directory: