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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
<?php header("Content-type:image/png"); #header("Content-Disposition:filename=image_code.png"); //定義 header 的文件格式為 png,第二個定義其實沒什麼用 // 開啟 session if (!isset($_SESSION)) { session_start(); } // 設定亂數種子 mt_srand((double)microtime()*1000000); // 驗證碼變數 $verification__session = ''; // 定義顯示在圖片上的文字,可以再加上大寫字母 $str = 'abcdefghijkmnpqrstuvwxyz23456789'; $l = strlen($str); //取得字串長度 //隨機取出 6 個字 for($i=0; $i<6; $i++){ $num=rand(0,$l-1); $verification__session.= $str[$num]; } // 將驗證碼記錄在 session 中 $_SESSION["verification__session"] = $verification__session; // 圖片的寬度與高度 $imageWidth = 160; $imageHeight = 50; // 建立圖片物件 $im = @imagecreatetruecolor($imageWidth, $imageHeight) or die("無法建立圖片!"); //主要色彩設定 // 圖片底色 $bgColor = imagecolorallocate($im, 255,255,255); // 文字顏色 $Color = imagecolorallocate($im, 200,132,43); // 干擾線條顏色 $gray1 = imagecolorallocate($im, 200,200,200); // 干擾像素顏色 $gray2 = imagecolorallocate($im, 200,200,200); //設定圖片底色 imagefill($im,0,0,$bgColor); //底色干擾線條
/*for($i=0; $i<10; $i++){ imageline($im,rand(0,$imageWidth),rand(0,$imageHeight), rand($imageHeight,$imageWidth),rand(0,$imageHeight),$gray1); } */ //利用true type字型來產生圖片 imagettftext($im, 25, 5, 10, 40, $Color, "fonts/AdobeGothicStd-Bold.otf", $verification__session); /* imagettftext (int im, int size, int angle, int x, int y, int col, string fontfile, string text) im 圖片物件 size 文字大小 angle 0度將會由左到右讀取文字,而更高的值表示逆時鐘旋轉 x y 文字起始座標 col 顏色物件 fontfile 字形路徑,為主機實體目錄的絕對路徑, 可自行設定想要的字型 text 寫入的文字字串 */ // 干擾像素 for($i=0;$i<90;$i++){ imagesetpixel($im, rand()%$imageWidth , rand()%$imageHeight , $gray2); } imagepng($im); imagedestroy($im);
|