Skip to content

Commit dc95788

Browse files
committed
Add check method and demo
1 parent 95609b1 commit dc95788

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

Captcha.php

+30-13
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,27 @@ class Captcha
1010
{
1111
private $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZaabcdefghijklmnopqrstuvwxyz1234567890";
1212
private $font;
13-
private $im;
13+
private $imageResource;
1414
private $level;
1515
private $code;
16+
private $caseSensitive;
1617

1718
/**
1819
* Captcha constructor.
1920
* @param int $level
21+
* @param boolean $caseSensitive
2022
*/
21-
public function __construct($level = 2)
23+
public function __construct($level = 2, $caseSensitive = false)
2224
{
2325
$this->level = $level;
26+
$this->caseSensitive = $caseSensitive;
2427

25-
// load the truetype font
26-
$this->font = realpath('./arial.ttf');
28+
// load the TrueType font
29+
$this->font = __DIR__.'/arial.ttf';
2730

2831
// create image
29-
$this->im = imagecreate(200, 77);
30-
imagecolorallocate($this->im, 255, 255, 255);
32+
$this->imageResource = imagecreate(200, 77);
33+
imagecolorallocate($this->imageResource, 255, 255, 255);
3134

3235
// generate
3336
$this->generate();
@@ -43,7 +46,7 @@ public function __construct($level = 2)
4346
*/
4447
public function getImageResource()
4548
{
46-
return $this->im;
49+
return $this->imageResource;
4750
}
4851

4952
/**
@@ -61,8 +64,8 @@ public function render()
6164
{
6265
header('Content-Type: image/png');
6366

64-
imagepng($this->im);
65-
imagedestroy($this->im);
67+
imagepng($this->imageResource);
68+
imagedestroy($this->imageResource);
6669
}
6770

6871
/**
@@ -81,10 +84,24 @@ private function generate()
8184

8285
// get font size
8386
$fontSize = ($this->level > 1) ? rand(20, 48) : 28;
84-
imagettftext($this->im, $fontSize, rand(-35, 35), 35 * $i, 55, imagecolorallocate($this->im, rand(0, 240), rand(0, 240), rand(0, 240)), $this->font, $char);
87+
imagettftext($this->imageResource, $fontSize, rand(-35, 35), 35 * $i, 55, imagecolorallocate($this->imageResource, rand(0, 240), rand(0, 240), rand(0, 240)), $this->font, $char);
8588
}
8689

87-
$this->code = $output;
90+
$this->code = ($this->caseSensitive) ? $output : strtolower($output);
91+
}
92+
93+
/**
94+
* @param $str
95+
* @return bool
96+
* check the code
97+
*/
98+
public function check($str){
99+
if (!$this->caseSensitive){
100+
return strtolower($str) == strtolower($this->code);
101+
}
102+
else{
103+
return $str == $this->code;
104+
}
88105
}
89106

90107
/**
@@ -95,11 +112,11 @@ private function addLines()
95112

96113
$lines = rand(1, 3);
97114
for ($i = 0; $i < $lines; $i++) {
98-
imageline($this->im, rand(0, 200), rand(0, -77), rand(0, 200), rand(77, 144), imagecolorallocate($this->im, rand(0, 240), rand(0, 240), rand(0, 240)));
115+
imageline($this->imageResource, rand(0, 200), rand(0, -77), rand(0, 200), rand(77, 144), imagecolorallocate($this->imageResource, rand(0, 240), rand(0, 240), rand(0, 240)));
99116
}
100117

101118
for ($i = 0; $i < 5 - $lines; $i++) {
102-
imageline($this->im, rand(0, -200), rand(0, 77), rand(200, 400), rand(0, 77), imagecolorallocate($this->im, rand(0, 240), rand(0, 240), rand(0, 240)));
119+
imageline($this->imageResource, rand(0, -200), rand(0, 77), rand(200, 400), rand(0, 77), imagecolorallocate($this->imageResource, rand(0, 240), rand(0, 240), rand(0, 240)));
103120
}
104121
}
105122
}

demo/action.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
session_start();
3+
if ($_SESSION['code'] == strtolower($_POST['code'])){
4+
echo "true";
5+
} else {
6+
echo "false";
7+
}

demo/image.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
session_start();
4+
require_once './../Captcha.php';
5+
6+
$captcha = new Captcha();
7+
$_SESSION['code'] = $captcha->getCode();
8+
$captcha->render();

demo/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport"
6+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Captcha Demo</title>
9+
10+
</head>
11+
<body>
12+
<form action="/demo/action.php" method="post">
13+
<input type="text" name="code" placeholder="please input the captcha">
14+
<img src="/demo/image.php" alt="">
15+
<input type="submit">
16+
</form>
17+
</body>
18+
</html>

0 commit comments

Comments
 (0)