PHP Text Captcha 是由免费的验证码脚本生成的文本图像。它用于保护您的登录表单、注册表单或任何表单免受垃圾邮件、滥用和黑客攻击。
在本文中,我们将学习如何在没有 google captcha API 的情况下创建 PHP Text Captacha。PHP Text Captcha 是用于创建复杂图像的开源脚本。
在这个文件中,我们创建了一个 phpcaptcha 类、hexTo RGB 和 ImageTTFCenter 函数来创建文本验证码的 imgae。
-
- /* phpcaptcha class, version 1.0
- created by www.tutorialswebsite.com (Pradeep Maurya)
- october 5, 2017
- */
-
- class phpcaptchaClass {
-
- public function phptext($text, $textColor, $backgroundColor = '', $fontSize, $imgWidth, $imgHeight, $dir, $fileName) {
- /* settings */
- $font = realpath(".") . '/monofont.ttf'; /* define font */
- $textColor = $this->hexToRGB($textColor);
-
- $im = imagecreatetruecolor($imgWidth, $imgHeight);
- $textColor = imagecolorallocate($im, $textColor['r'], $textColor['g'], $textColor['b']);
-
- if ($backgroundColor == '') {/* select random color */
- $colorCode = array('#56aad8', '#61c4a8', '#d3ab92');
- $backgroundColor = $this->hexToRGB($colorCode[rand(0, count($colorCode) - 1)]);
- $backgroundColor = imagecolorallocate($im, $backgroundColor['r'], $backgroundColor['g'], $backgroundColor['b']);
- } else {/* select background color as provided */
- $backgroundColor = $this->hexToRGB($backgroundColor);
- $backgroundColor = imagecolorallocate($im, $backgroundColor['r'], $backgroundColor['g'], $backgroundColor['b']);
- }
-
- imagefill($im, 0, 0, $backgroundColor);
- list($x, $y) = $this->ImageTTFCenter($im, $text, $font, $fontSize);
- imagettftext($im, $fontSize, 0, $x, $y, $textColor, $font, $text);
- if (imagejpeg($im, $dir . $fileName, 90)) {/* save image as JPG */
- return json_encode(array('status' => TRUE, 'image' => $dir . $fileName));
- imagedestroy($im);
- }
- }
-
- public function phpcaptcha($textColor, $backgroundColor, $imgWidth, $imgHeight, $noiceLines = 0, $noiceDots = 0, $noiceColor = '#162453') {
- /* Settings */
- $text = $this->random();
- $font = realpath(".") . '/monofont.ttf'; /* define font */
- $textColor = $this->hexToRGB($textColor);
- $fontSize = $imgHeight * 0.75;
-
- $im = imagecreatetruecolor($imgWidth, $imgHeight);
- $textColor = imagecolorallocate($im, $textColor['r'], $textColor['g'], $textColor['b']);
-
- $backgroundColor = $this->hexToRGB($backgroundColor);
- $backgroundColor = imagecolorallocate($im, $backgroundColor['r'], $backgroundColor['g'], $backgroundColor['b']);
-
- /* generating lines randomly in background of image */
- if ($noiceLines > 0) {
- $noiceColor = $this->hexToRGB($noiceColor);
- $noiceColor = imagecolorallocate($im, $noiceColor['r'], $noiceColor['g'], $noiceColor['b']);
- for ($i = 0; $i < $noiceLines; $i++) {
- imageline($im, mt_rand(0, $imgWidth), mt_rand(0, $imgHeight),
- mt_rand(0, $imgWidth), mt_rand(0, $imgHeight), $noiceColor);
- }
- }
-
- if ($noiceDots > 0) {/* generating the dots randomly in background */
- for ($i = 0; $i < $noiceDots; $i++) {
- imagefilledellipse($im, mt_rand(0, $imgWidth),
- mt_rand(0, $imgHeight), 3, 3, $textColor);
- }
- }
-
- imagefill($im, 0, 0, $backgroundColor);
- list($x, $y) = $this->ImageTTFCenter($im, $text, $font, $fontSize);
- imagettftext($im, $fontSize, 0, $x, $y, $textColor, $font, $text);
-
- imagejpeg($im, NULL, 90); /* Showing image */
- header('Content-Type: image/jpeg'); /* defining the image type to be shown in browser widow */
- imagedestroy($im); /* Destroying image instance */
- if (isset($_SESSION)) {
- $_SESSION['captcha_code'] = $text; /* set random text in session for captcha validation */
- }
- }
-
- /* for random string */
-
- protected function random($characters = 6, $letters = '23456789bcdfghjkmnpqrstvwxyz') {
- $str = '';
- for ($i = 0; $i < $characters; $i++) {
- $str .= substr($letters, mt_rand(0, strlen($letters) - 1), 1);
- }
- return $str;
- }
-
- /* function to convert hex value to rgb array */
-
- protected function hexToRGB($colour) {
- if ($colour[0] == '#') {
- $colour = substr($colour, 1);
- }
- if (strlen($colour) == 6) {
- list( $r, $g, $b ) = array($colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5]);
- } elseif (strlen($colour) == 3) {
- list( $r, $g, $b ) = array($colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2]);
- } else {
- return false;
- }
- $r = hexdec($r);
- $g = hexdec($g);
- $b = hexdec($b);
- return array('r' => $r, 'g' => $g, 'b' => $b);
- }
-
- /* function to get center position on image */
-
- protected function ImageTTFCenter($image, $text, $font, $size, $angle = 8) {
- $xi = imagesx($image);
- $yi = imagesy($image);
- $box = imagettfbbox($size, $angle, $font, $text);
- $xr = abs(max($box[2], $box[4]));
- $yr = abs(max($box[5], $box[7]));
- $x = intval(($xi - $xr) / 2);
- $y = intval(($yi + $yr) / 2);
- return array($x, $y);
- }
-
- }
-
- ?>
该文件用于创建表单数据并用于在提交表单后验证验证码图像。
- session_start();
-
- if (isset($_POST['Submit'])) {
- // code for check server side validation
- if (empty($_SESSION['captcha_code']) || strcasecmp($_SESSION['captcha_code'], $_POST['captcha_code']) != 0) {
- $msg = "The Validation code does not match!"; // Captcha verification is incorrect.
- } else {// Captcha verification is Correct. Final Code Execute here!
- $msg = "The Validation code has been matched.";
- }
- }
- ?>
-
- "utf-8">
-
PHP Secure text Captcha. - "./css/style.css" rel="stylesheet">
-
-
-
- "frame0">
-
PHP Secure Text Captcha Demo.
-
More tutorials "https://tutorialswebsite.com">www.tutorialswebsite.com
-
-
-
-
-
"400" border="0" align="center" cellpadding="5" cellspacing="1" class="table">- php if (isset($msg)) { ?>
-
-
"2" align="center" valign="top"> echo $msg; ?> -
- } ?>
-
-
"right" valign="top"> Validation code: -
"captcha.php?rand=" id='captchaimg'>
-
-
- "captcha_code" name="captcha_code" type="text">
-
- Can't read the image? click here to refresh.
-
-
-
-
-
-
-
-
该文件用于创建验证码图像。在这个文件中,我们包含“phpcaptchaClass.php”文件并创建一个phpcaptchaClass对象。
phpcaptcha():-此函数根据参数创建带有文本和线条或噪声的图像。
-
- session_start();
- include("./phpcaptchaClass.php");
-
- /* create class object */
- $phptextObj = new phpcaptchaClass();
- /* phptext function to genrate image with text */
- $phptextObj->phpcaptcha('#000', '#fff', 120, 40, 10, 25);
- ?>
在示例脚本中,静态图像显示在 3D 图像库中。