Skip to content

Instantly share code, notes, and snippets.

@emmykolic
Created December 21, 2022 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmykolic/6ee121a8d6c807e1365403451bf24e76 to your computer and use it in GitHub Desktop.
Save emmykolic/6ee121a8d6c807e1365403451bf24e76 to your computer and use it in GitHub Desktop.
The Page that Generates the Captcha that is viewed on the Index.php
<?php
session_start();
/*Create a 220x35 image*/
$im = imagecreatetruecolor(220, 35);
/*Color code for orange*/
$orange = imagecolorallocate($im, 0xFF, 0x8c, 0x00);
/*Color code for white*/
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
/*Generate a random string using md5*/
$md5_hash = md5(rand(0,999));
/*Trim the string down to 6 characters*/
$captcha_code = substr($md5_hash, 15, 6);
//Store the value of the generated captcha code in session
$_SESSION['captcha'] = $captcha_code;
/* Set the background as orange */
imagefilledrectangle($im, 0, 0, 220, 35, $orange);
/*Path where TTF font file is present*/
$font_file = getcwd() . '/fonts/Pixelation.ttf';
/* Draw our randomly generated code*/
imagefttext($im, 30, 0, 5, 30, $white, $font_file, $captcha_code);
/* Output the image to the browser*/
header('Content-Type: image/png');
imagepng($im);
/*Destroy*/
imagedestroy($im);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment