用自己想要的字體來做 ASP 圖片驗證
哈~好久沒有發文章了~
小驢~前一陣子換了工作,最近還不錯,也比較有空~
不囉嗦,來說一下今天的主題~
因為工作的關係,最近需要使用ASP、ASP.NET的圖片驗證(而且不能使用元件)
ASP.NET本身有System.Drawing可以使用,直接把字體轉成圖檔、繪製雜點、使用漸層...等不是難題
但是咧~ASP....就難了~網路上可以找到最多的範例,就底下這個吧~
ASP圖片驗證
01 <%
02 Call Com_CreatValidCode()
03
04 Sub Com_CreatValidCode()
05 Response.Expires = -9999
06 Response.AddHeader "Pragma","no-cache"
07 Response.AddHeader "cache-ctrol","no-cache"
08 Response.ContentType = "Image/BMP"
09 Randomize
10 Dim i, ii, iii
11 Const cAmount = 36
12 Const cCode = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13 Dim vColorData(2)
14 a=CInt(Int(Rnd * 256))
15 b=CInt(Int(Rnd * 256))
16 c=CInt(Int(Rnd * 256))
17 vColorData(0) = ChrB(a) & ChrB(b) & ChrB(c) '隨機產生顏色
18 vColorData(1) = ChrB(255) & ChrB(255) & ChrB(255) '白色
19 Dim vCode(4), vCodes
20 For i = 0 To 3
21 vCode(i) = Int(Rnd * cAmount) '隨機產生0~35
22 vCodes = vCodes & Mid(cCode, vCode(i) + 1, 1) '取得對應字元並串接
23 Next
24 session("CheckCode") = vCodes 'vCodes既為產生的字串
25 Dim vNumberData(36) 'vNumberData(0)對應字元0的圖像 'vNumberData(10)對應字元10的圖像
26 vNumberData(0) = "1110000111110111101111011110111101001011110100101111010010111101001011110111101111011110111110000111"
27 vNumberData(1) = "1111011111110001111111110111111111011111111101111111110111111111011111111101111111110111111100000111"
28 vNumberData(2) = "1110000111110111101111011110111111111011111111011111111011111111011111111011111111011110111100000011"
29 vNumberData(3) = "1110000111110111101111011110111111110111111100111111111101111111111011110111101111011110111110000111"
30 vNumberData(4) = "1111101111111110111111110011111110101111110110111111011011111100000011111110111111111011111111000011"
31 vNumberData(5) = "1100000011110111111111011111111101000111110011101111111110111111111011110111101111011110111110000111"
32 vNumberData(6) = "1111000111111011101111011111111101111111110100011111001110111101111011110111101111011110111110000111"
33 vNumberData(7) = "1100000011110111011111011101111111101111111110111111110111111111011111111101111111110111111111011111"
34 vNumberData(8) = "1110000111110111101111011110111101111011111000011111101101111101111011110111101111011110111110000111"
35 vNumberData(9) = "1110001111110111011111011110111101111011110111001111100010111111111011111111101111011101111110001111"
36 vNumberData(10) = "1111011111111101111111101011111110101111111010111111101011111100000111110111011111011101111000100011"
37 vNumberData(11) = "1000000111110111101111011110111101110111110000111111011101111101111011110111101111011110111000000111"
38 vNumberData(12) = "1110000011110111101110111110111011111111101111111110111111111011111111101111101111011101111110001111"
39 vNumberData(13) = "1000001111110111011111011110111101111011110111101111011110111101111011110111101111011101111000001111"
40 vNumberData(14) = "1000000111110111101111011011111101101111110000111111011011111101101111110111111111011110111000000111"
41 vNumberData(15) = "1000000111110111101111011011111101101111110000111111011011111101101111110111111111011111111000111111"
42 vNumberData(16) = "1110000111110111011110111101111011111111101111111110111111111011100011101111011111011101111110001111"
43 vNumberData(17) = "1000100011110111011111011101111101110111110000011111011101111101110111110111011111011101111000100011"
44 vNumberData(18) = "1100000111111101111111110111111111011111111101111111110111111111011111111101111111110111111100000111"
45 vNumberData(19) = "1110000011111110111111111011111111101111111110111111111011111111101111111110111110111011111000011111"
46 vNumberData(20) = "1000100011110111011111011011111101011111110001111111010111111101101111110110111111011101111000100011"
47 vNumberData(21) = "1000111111110111111111011111111101111111110111111111011111111101111111110111111111011110111000000011"
48 vNumberData(22) = "1000100011110010011111001001111100100111110101011111010101111101010111110101011111010101111001010011"
49 vNumberData(23) = "1000100011110011011111001101111101010111110101011111010101111101100111110110011111011001111000110111"
50 vNumberData(24) = "1110001111110111011110111110111011111011101111101110111110111011111011101111101111011101111110001111"
51 vNumberData(25) = "1000000111110111101111011110111101111011110000011111011111111101111111110111111111011111111000111111"
52 vNumberData(26) = "1110001111110111011110111110111011111011101111101110111110111011111011101001101111011001111110001011"
53 vNumberData(27) = "1000001111110111011111011101111101110111110000111111010111111101101111110110111111011101111000110011"
54 vNumberData(28) = "1110000011110111101111011110111101111111111001111111111001111111111011110111101111011110111100000111"
55 vNumberData(29) = "1000000011101101101111110111111111011111111101111111110111111111011111111101111111110111111110001111"
56 vNumberData(30) = "1000100011110111011111011101111101110111110111011111011101111101110111110111011111011101111110001111"
57 vNumberData(31) = "1000100011110111011111011101111101110111111010111111101011111110101111111010111111110111111111011111"
58 vNumberData(32) = "1001010011110101011111010101111101010111110101011111001001111110101111111010111111101011111110101111"
59 vNumberData(33) = "1000100011110111011111101011111110101111111101111111110111111110101111111010111111011101111000100011"
60 vNumberData(34) = "1000100011110111011111011101111110101111111010111111110111111111011111111101111111110111111110001111"
61 vNumberData(35) = "1100000011110111011111111101111111101111111110111111110111111111011111111011111111101110111100000011"
62 '底下的程式 在產生 圖檔
63 Response.BinaryWrite ChrB(66) & ChrB(77) & ChrB(230) & ChrB(4) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(54) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(40) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(40) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(10) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(1) & ChrB(0)
64 Response.BinaryWrite ChrB(24) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(176) & ChrB(4) & ChrB(0) & ChrB(0) & ChrB(18) & ChrB(11) & ChrB(0) & ChrB(0) & ChrB(18) & ChrB(11) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0)
65 For i = 9 To 0 Step -1
66 For ii = 0 To 3
67 For iii = 1 To 10
68 'If Rnd * 99 + 1 <3 Then ' < 右方的數值越大 代表產生的圖越看不清楚原本的字
69 ' Response.BinaryWrite vColorData(0) '使用隨機產生的顏色
70 'Else
71 ' Response.BinaryWrite vColorData(Mid(vNumberData(vCode(ii)), i * 10 + iii, 1))
72 'End If
73 '0 <= vCode(X) <= 35; 91~100 .. 81~90 ...... 1~10;
74 Response.BinaryWrite vColorData(Mid(vNumberData(vCode(ii)), i * 10 + iii, 1))
75 Next
76 Next
77 Next
78 End Sub
79 %>
01 <%
02 Call Com_CreatValidCode()
03
04 Sub Com_CreatValidCode()
05 Response.Expires = -9999
06 Response.AddHeader "Pragma","no-cache"
07 Response.AddHeader "cache-ctrol","no-cache"
08 Response.ContentType = "Image/BMP"
09 Randomize
10 Dim i, ii, iii
11 Const cAmount = 36
12 Const cCode = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13 Dim vColorData(2)
14 a=CInt(Int(Rnd * 256))
15 b=CInt(Int(Rnd * 256))
16 c=CInt(Int(Rnd * 256))
17 vColorData(0) = ChrB(a) & ChrB(b) & ChrB(c) '隨機產生顏色
18 vColorData(1) = ChrB(255) & ChrB(255) & ChrB(255) '白色
19 Dim vCode(4), vCodes
20 For i = 0 To 3
21 vCode(i) = Int(Rnd * cAmount) '隨機產生0~35
22 vCodes = vCodes & Mid(cCode, vCode(i) + 1, 1) '取得對應字元並串接
23 Next
24 session("CheckCode") = vCodes 'vCodes既為產生的字串
25 Dim vNumberData(36) 'vNumberData(0)對應字元0的圖像 'vNumberData(10)對應字元10的圖像
26 vNumberData(0) = "1110000111110111101111011110111101001011110100101111010010111101001011110111101111011110111110000111"
27 vNumberData(1) = "1111011111110001111111110111111111011111111101111111110111111111011111111101111111110111111100000111"
28 vNumberData(2) = "1110000111110111101111011110111111111011111111011111111011111111011111111011111111011110111100000011"
29 vNumberData(3) = "1110000111110111101111011110111111110111111100111111111101111111111011110111101111011110111110000111"
30 vNumberData(4) = "1111101111111110111111110011111110101111110110111111011011111100000011111110111111111011111111000011"
31 vNumberData(5) = "1100000011110111111111011111111101000111110011101111111110111111111011110111101111011110111110000111"
32 vNumberData(6) = "1111000111111011101111011111111101111111110100011111001110111101111011110111101111011110111110000111"
33 vNumberData(7) = "1100000011110111011111011101111111101111111110111111110111111111011111111101111111110111111111011111"
34 vNumberData(8) = "1110000111110111101111011110111101111011111000011111101101111101111011110111101111011110111110000111"
35 vNumberData(9) = "1110001111110111011111011110111101111011110111001111100010111111111011111111101111011101111110001111"
36 vNumberData(10) = "1111011111111101111111101011111110101111111010111111101011111100000111110111011111011101111000100011"
37 vNumberData(11) = "1000000111110111101111011110111101110111110000111111011101111101111011110111101111011110111000000111"
38 vNumberData(12) = "1110000011110111101110111110111011111111101111111110111111111011111111101111101111011101111110001111"
39 vNumberData(13) = "1000001111110111011111011110111101111011110111101111011110111101111011110111101111011101111000001111"
40 vNumberData(14) = "1000000111110111101111011011111101101111110000111111011011111101101111110111111111011110111000000111"
41 vNumberData(15) = "1000000111110111101111011011111101101111110000111111011011111101101111110111111111011111111000111111"
42 vNumberData(16) = "1110000111110111011110111101111011111111101111111110111111111011100011101111011111011101111110001111"
43 vNumberData(17) = "1000100011110111011111011101111101110111110000011111011101111101110111110111011111011101111000100011"
44 vNumberData(18) = "1100000111111101111111110111111111011111111101111111110111111111011111111101111111110111111100000111"
45 vNumberData(19) = "1110000011111110111111111011111111101111111110111111111011111111101111111110111110111011111000011111"
46 vNumberData(20) = "1000100011110111011111011011111101011111110001111111010111111101101111110110111111011101111000100011"
47 vNumberData(21) = "1000111111110111111111011111111101111111110111111111011111111101111111110111111111011110111000000011"
48 vNumberData(22) = "1000100011110010011111001001111100100111110101011111010101111101010111110101011111010101111001010011"
49 vNumberData(23) = "1000100011110011011111001101111101010111110101011111010101111101100111110110011111011001111000110111"
50 vNumberData(24) = "1110001111110111011110111110111011111011101111101110111110111011111011101111101111011101111110001111"
51 vNumberData(25) = "1000000111110111101111011110111101111011110000011111011111111101111111110111111111011111111000111111"
52 vNumberData(26) = "1110001111110111011110111110111011111011101111101110111110111011111011101001101111011001111110001011"
53 vNumberData(27) = "1000001111110111011111011101111101110111110000111111010111111101101111110110111111011101111000110011"
54 vNumberData(28) = "1110000011110111101111011110111101111111111001111111111001111111111011110111101111011110111100000111"
55 vNumberData(29) = "1000000011101101101111110111111111011111111101111111110111111111011111111101111111110111111110001111"
56 vNumberData(30) = "1000100011110111011111011101111101110111110111011111011101111101110111110111011111011101111110001111"
57 vNumberData(31) = "1000100011110111011111011101111101110111111010111111101011111110101111111010111111110111111111011111"
58 vNumberData(32) = "1001010011110101011111010101111101010111110101011111001001111110101111111010111111101011111110101111"
59 vNumberData(33) = "1000100011110111011111101011111110101111111101111111110111111110101111111010111111011101111000100011"
60 vNumberData(34) = "1000100011110111011111011101111110101111111010111111110111111111011111111101111111110111111110001111"
61 vNumberData(35) = "1100000011110111011111111101111111101111111110111111110111111111011111111011111111101110111100000011"
62 '底下的程式 在產生 圖檔
63 Response.BinaryWrite ChrB(66) & ChrB(77) & ChrB(230) & ChrB(4) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(54) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(40) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(40) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(10) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(1) & ChrB(0)
64 Response.BinaryWrite ChrB(24) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(176) & ChrB(4) & ChrB(0) & ChrB(0) & ChrB(18) & ChrB(11) & ChrB(0) & ChrB(0) & ChrB(18) & ChrB(11) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0)
65 For i = 9 To 0 Step -1
66 For ii = 0 To 3
67 For iii = 1 To 10
68 'If Rnd * 99 + 1 <3 Then ' < 右方的數值越大 代表產生的圖越看不清楚原本的字
69 ' Response.BinaryWrite vColorData(0) '使用隨機產生的顏色
70 'Else
71 ' Response.BinaryWrite vColorData(Mid(vNumberData(vCode(ii)), i * 10 + iii, 1))
72 'End If
73 '0 <= vCode(X) <= 35; 91~100 .. 81~90 ...... 1~10;
74 Response.BinaryWrite vColorData(Mid(vNumberData(vCode(ii)), i * 10 + iii, 1))
75 Next
76 Next
77 Next
78 End Sub
79 %>
字體固定....大小固定....只能設定雜點....
那~因為系統沒得選,就是ASP,但是要加入這個功能~
好吧那...小驢來弄一個,借用vb.net先產生字體字串(vNumberData),放到圖片陣列裡面
再修改一下ASP的程式碼~現在....有漸層,前景雜點,背景雜線,字體邊緣柔化,可以挑選自己想要的字體.....甚至使用中文字~
這是產生的圖形 哈~
這個呢~是用來產生vNumberData的程式畫面~
最後小驢附上產生vNumberData的程式、asp網頁,關於網頁的修改裡面有說明了~
有興趣的大大請慢用~