1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  | function phpgd()
{
	$code = "";
	$charset = "ABCDEFGHJKMNPQRSTUWXY3456789"; // 去掉 I O Z 0 1 2
	$charset_length = strlen($charset);
	for ($i = 0; $i < 4; $i++) {
		$rand_index = rand(0, $charset_length - 1);
		$rand_char = substr($charset, $rand_index, 1);
		$code .= $rand_char;
	}
	$_SESSION["captcha"] = $code;
	// 展示验证码图片
	$image_width = 120;
	$image_height = 40;
	$image = imagecreatetruecolor($image_width, $image_height);
	$bg_color = imagecolorallocate($image, 255, 255, 255);
	imagefill($image, 0, 0, $bg_color);
	$font_size = 20;
	$font_color = imagecolorallocate($image, 0, 0, 0);
	$x = 10;
	$y = ($image_height - $font_size) / 2 + $font_size;
	for ($i = 0; $i < 4; $i++) {
		$char = substr($code, $i, 1);
		imagettftext($image, $font_size, rand(-10, 10), $x, $y, $font_color, "/usr/share/fonts/open-sans/OpenSans-Bold.ttf", $char);
		$x += $font_size + rand(5, 10);
	}
	$x = $image_width;
	$y = $image_height;
	// 干扰线
	for ($i = 0; $i < 8; $i ++) {
		$lineColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
		imageline($image, rand(0, $x), 0, rand(0, $x), $y, $lineColor);
	}
	// 干扰点
	for ($i = 0; $i < 200; $i ++) {
		$fontColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
		imagesetpixel($image, rand(0, $x), rand(0, $y), $fontColor);
	}
	header('Content-Type: image/png');
	imagepng($image);
	imagedestroy($image);
}
  |