Skip to content

Instantly share code, notes, and snippets.

@Anan5a
Created March 15, 2018 05:48
Show Gist options
  • Save Anan5a/1bbf276d9d5a8de0cbcecf7dca02490f to your computer and use it in GitHub Desktop.
Save Anan5a/1bbf276d9d5a8de0cbcecf7dca02490f to your computer and use it in GitHub Desktop.
Super Simple Captcha script
<?php
/*
*
*Author github.com/Anan5a
*Description Simple Captcha generator script
*/
//require_once './Fnc.php'; //application specific
define('CODESTR','012ABC3456789DEFGHIJKLMNOPQRSTUVWXYZ');
$font = './X51-Outline.ttf';
$hash = $_GET['key'];
if (!is_array($_SESSION['captcha'])) {
$_SESSION['captcha']=[];
}
if (!array_key_exists($hash, $_SESSION['captcha'])) {
//assign values
$str = str_shuffle(CODESTR);
$code = substr($str, 0, rand(3, 6));
$validity = time()+360;
//set values
$_SESSION['captcha'][$hash] = [
'code'=>$code,
'validity'=>$validity,
];
//draw captcha
$image = imagecreatetruecolor(200, 50);
$im_color = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, 200, 50, $im_color);
$pixel = imagecolorallocate($image, 0, 0, 255);
for ($i = 0; $i < 1000 ;$i ++) {
imagesetpixel($image, rand() %200, rand() %50, $pixel);
}
$txt_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, 30, (rand(-5, 5)), 10, 40, $txt_color, $font, $code);
imagepng($image);
$im2 = $image;
imagedestroy($image);
//print
echo $image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment