PHP Script to Create Your Own Captcha

What is Captcha ?
Captcha is a type of challenge–response test used in computing to determine whether or not the user is human. CAPTCHA is mainly used as a security check to ensure only human users can pass through. Generally, computers or bots are not capable of solving a captcha.
In general, Captchas helps our website to get rid of automated crawlers and bots that’s eating and wasting our bandwidth for whatever purposes.
The following PHP script creates a random captcha.
<?php function generateRandomString($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; } function generateCaptchaImage($text = 'good'){ // Set the content-type header('Content-Type: image/png'); $width = 200; $height = 30; // Create the image $im = imagecreatetruecolor($width, $height); // Create some colors $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 128, 128, 128); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 399, 29, $white); //ADD NOISE - DRAW background squares $square_count = 6; for($i = 0; $i < $square_count; $i++){ $cx = rand(0,$width); $cy = (int)rand(0, $width/2); $h = $cy + (int)rand(0, $height/5); $w = $cx + (int)rand($width/3, $width); imagefilledrectangle($im, $cx, $cy, $w, $h, $white); } //ADD NOISE - DRAW ELLIPSES $ellipse_count = 5; for ($i = 0; $i < $ellipse_count; $i++) { $cx = (int)rand(-1*($width/2), $width + ($width/2)); $cy = (int)rand(-1*($height/2), $height + ($height/2)); $h = (int)rand($height/2, 2*$height); $w = (int)rand($width/2, 2*$width); imageellipse($im, $cx, $cy, $w, $h, $grey); } // Replace path by your own font path $font = 'ThisisKeSha.ttf'; // Add some shadow to the text imagettftext($im, 20, 0, 11, 21, $grey, $font, $text); // Add the text imagettftext($im, 20, 0, 10, 20, $black, $font, $text); // Using imagepng() results in clearer text compared with imagejpeg() imagepng($im); imagedestroy($im); } $randomString = generateRandomString(); generateCaptchaImage($randomString); ?>
Follow this video for complete guidance :