-
-
Save odixon/06473de1a97c86fed78fd95444fa0c77 to your computer and use it in GitHub Desktop.
uppercase alphanumeric CAPTCHA class with imagettftext
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 | |
// captcha class by noidsit | |
// generate captcha uppercase alphanumeric | |
// if you want to use upper/lower case alphanumeric add lower case alphabet | |
// usage string = new Captcha(16,230,32,6); //fontwidth, image witdh, image height, how many characters | |
// string->captgen(); to generate captcha | |
class Captcha { | |
private $fontwidth; | |
private $width; | |
private $height; | |
private $charcount; | |
public $captsession; | |
public function __construct($ukuranhuruf,$lebar,$tinggi,$jumlahhuruf) { | |
$this->fontwidth = $ukuranhuruf; | |
$this->width = $lebar; | |
$this->height = $tinggi; | |
$this->charcount = $jumlahhuruf; | |
} | |
public function captcode() { | |
$randomstring = ''; | |
$chars = array( | |
'1', '2', '3', '4', '5','6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); | |
for ($rand = 1; $rand <= $this->charcount; $rand++) { | |
$random = rand(0, count($chars) - 1); | |
$randomstring .= $chars[$random]; | |
} | |
$_SESSION['captchacode'] = $randstr; // store this value in our PHP session | |
return $randstr; | |
} | |
public function captgen() { | |
$im = imagecreate($this->width,$this->height); | |
$code = $this->captcode(); | |
$this->captsession = $code; | |
$background_color = imagecolorallocate($im, 255,255,255); | |
$mid = imagecolorallocate($im, 33,153,215); | |
$color = imagecolorallocate($im, 204,204,204); | |
$fw = $this->fontwidth; // width of a character | |
$l = strlen($code); // number of characters | |
$tw = $l * $fw; // text width | |
$th = $this->fontwidth * 0.6; | |
$iw = imagesx($im); // image width | |
$xpos = ($iw - $tw)/2; | |
$ypos = $this->height - $th; | |
$font = ABSPATH.'asset/themes/cariperawat/fonts/RobotoCondensed-Regular.ttf'; //change this to your ttf font location | |
imagettftext ($im, $this->fontwidth, 0, $xpos, $ypos, $mid, $font, $code); | |
drawBorder($im,$color, 1); // draw border for image, optional - delete if not required | |
ob_start(); //start byte stream | |
imagejpeg($im, NULL, 100); | |
$rawImageBytes = ob_get_clean(); | |
imagedestroy( $im ); | |
?> | |
<img src="data:image/jpeg;base64,<?php echo base64_encode( $rawImageBytes ); ?>" class="mt5 fleft inl prel" alt="Kode Keamanan Captcha" /> //remember to change to your class | |
<input type="button" id="reloadcapt" name="reloadcapt" class="borbb fwh blue fleft inl prel ps5 ms3 mt5 pt5 cpointer" value="Ganti Kode"/> //remember to change to your class | |
<?php | |
} | |
public function captses() { | |
return $this->captsession; //output our captcha code to later use, needed for captcha input comparison | |
} | |
} // end class | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment