Last active
September 5, 2020 17:55
-
-
Save ToshY/683c916ae5fbd0dd556cb1c7945bae1d to your computer and use it in GitHub Desktop.
Create an alphanumeric captcha with different text -and linecolours
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Create an alphanumeric captcha the easy way - by ToshY | 05-05-2019 */ | |
function imageCaptcha($chars = 6, $lines, $bg_colour = array('R'=>204,'G'=>153,'B'=>255), $line_colours = array(array('R'=>153,'G'=>51,'B'=>102)), $txt_colours = array(array('R'=>0,'G'=>0,'B'=>0),array('R'=>255,'G'=>255,'B'=>255)), $fonts = array('arial.ttf')){ | |
// dimensions / fontsize based on amount of characters | |
$MW = round($chars * ( 100 / 3) ); $MH = round($MW / 3); $fs = round( ( 30 * (1 - exp( -0.005 * $MW ) ) ) , 2); | |
// init image with user dimensions | |
$img = imagecreatetruecolor( $MW, $MH ); | |
// set bg | |
$bg = call_user_func_array('imagecolorallocate', array_merge( array($img), $bg_colour ) ); | |
imagefill( $img, 0, 0, $bg ); | |
// line colours array | |
$lineclrs = array(); $cl = count($line_colours); | |
for($i = 0; $i < $cl; $i++){ | |
$lineclrs[] = call_user_func_array('imagecolorallocate', array_merge( array($img), $line_colours[$i] ) ); | |
} | |
// add lines to img | |
for($j = 0; $j < $lines; $j++) { | |
imagesetthickness( $img, rand( 1, floor($fs / 5) ) ); | |
imageline( $img, rand( 0, $MW ), 0, rand( 0, $MW ), $MH, $lineclrs[array_rand($lineclrs)] ); | |
} | |
// text colours array | |
$txtclrs = array(); $ct = count($txt_colours); | |
for($k = 0; $k < $ct; $k++){ | |
$txtclrs[] = call_user_func_array('imagecolorallocate', array_merge( array($img), $txt_colours[$k] ) ); | |
} | |
// get random letter and add to img | |
$strcapt = ''; | |
for($l = 0, $x = 8; $l < $chars; $l++, $x += round((( $MW - ( 1.5 * $fs ) ) / ($chars - 1)))){ | |
// get random letter | |
$strcapt .= ($rstr = gRS(1,'101')); | |
// add letter to image | |
imagettftext($img, $fs, rand(-33,33), $x, rand( $fs + 5, $MH - 5), $txtclrs[array_rand($txtclrs)], $fonts[array_rand($fonts)], $rstr); | |
} | |
// add to session | |
$_SESSION['img_captcha'] = $strcapt; | |
// display img | |
header('Content-type: image/png'); | |
imagepng($img); | |
imagedestroy($img); | |
} | |
function gRS($length, $opt = '111') { | |
$r = ''; | |
//numbers only | |
$r .= ( ( (int) $opt[0] === 1) ? '012345678923456789' : ''); | |
//numbers with small letters | |
$r .= ( ( (int) $opt[1] === 1) ? 'abcdefghijklmnopqrstuvwxyz' : ''); | |
//numbers with small and capital letters | |
$r .= ( ( (int) $opt[2] === 1) ? 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' : ''); | |
return ( ($r) ? substr( str_shuffle( str_repeat( $r, ceil( $length / strlen( $r ) ) ) ), 1, $length) : NULL); | |
} | |
echo '<img src="'.imageCaptcha(50, 20, array(50,100,200), array(array(100,20,50),array(10,50,200),array(70,10,50)), array(array(66, 244, 158), array(247, 89, 49), array(255, 255, 255), array(0, 0 ,0)), array('/path/to/a/font.ttf','/path/to/another/font.ttf')).'">'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment