現在越來越多的網站都開始使用漢字驗證碼了,既增加了我們國人的親切感,同時也增加了機器破解的難度,這里我就簡單粗暴的說一下。。。
創建背景畫布
1
2
3
|
$image = imagecreatetruecolor(200, 60); $background = imagecolorallocate( $image , 255, 255, 255); imagefill( $image , 0, 0, $background ); |
畫干擾點
1
2
3
4
5
6
|
for ( $i =0; $i < 300; $i ++) { $pixColor = imagecolorallocate( $image , rand(150, 240), rand(150, 240), rand(150, 240)); $pixX = rand(10, 190); $pixY = rand(5, 55); imagesetpixel( $image , $pixX , $pixY , $pixColor ); } |
畫干擾線
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//4條水平線 for ( $i =0; $i < 5; $i ++) { $lineColor = imagecolorallocate( $image , rand(50, 150), rand(50, 150), rand(50, 150)); $lineX1 = 0; $lineX2 = 300; $lineY1 = ( $i + 1) * 12; $lineY2 = ( $i + 1) * 12; imageline( $image , $lineX1 , $lineY1 , $lineX2 , $lineY2 , $lineColor ); } //10條垂直線 for ( $i =0; $i < 30; $i ++) { $lineColor = imagecolorallocate( $image , rand(50, 150), rand(50, 150), rand(50, 150)); $lineX1 = ( $i + 1) * 10; $lineX2 = ( $i + 1) * 10; $lineY1 = 0; $lineY2 = 60; imageline( $image , $lineX1 , $lineY1 , $lineX2 , $lineY2 , $lineColor ); } |
畫漢字
1
2
3
4
5
6
7
|
$text = array ( '梔' , '子' , '花' , '開' ); for ( $i =0; $i < 4; $i ++) { $textColor = imagecolorallocate( $image , rand(20, 100), rand(20, 100), rand(20, 100)); $textX = $i * 50 + 10; $textY = rand(40, 60); imagettftext( $image , 30, rand(20, 50), $textX , $textY , $textColor , "/Library/Fonts/華文仿宋.ttf" , $text [ $i ]); } |
這里注意一下,字體文件一定要支持中文的
編碼要使用utf-8,gbk的中文記得要轉嗎【iconv函數可以幫助你】
輸出圖像
1
2
|
header( "Content-Type:image/png" ); imagepng( $image ); |
銷毀資源
1
|
imagedestroy( $image ); |
經過粗略的搞吧搞吧,中文驗證碼也就顯示出來了,當然一般網站使用的時候會有一個漢字庫種子,從里面隨機取出特定個數的漢字顯示,最后就是記錄到session進行驗證了。
以上所述就是本文的全部內容了,希望大家能夠喜歡。