- header("Content-type:image/jpeg")
-
複製代碼GD庫中有對應的image類型 imagejpeg(),imagegif(),imagepang() 5,imageline畫線函數 iamgeline(resource image,int x1,int y1,int x2,int y2,int color); image ---圖片 x1 ---啟始座標 y1 x2 ---終點座標 y2 6,imagesetpixel畫點函數 imagesetpixel(resource image,int x,int y,int color) 7,imagettftext帶字型的寫入函數 imagettftext(resource image,float size,float angle,int x,int y,int color,string fontfile,string text) 8,php驗證碼插入中文的方法 iconv("gb2312","utf-8","字串"); //首先要將文字轉換成utf-8格式 9,隨機函數 1,rand([int min,int max]) //rand(1,4) 產生1-4的數 2, dechex(十進位數) //轉換為十六進位 以上介紹了php中gd庫函數的一些常用方法,以及在php驗證碼中插入中文的方法,說到php驗證碼中顯示中文的問題,大家可以參考之前介紹的 PHP中文漢字驗證碼(執行個體) 這篇文章。 另外,有興趣的朋友,還可以研究下php 隨機驗證碼與 php 圖片驗證碼的實現方法,對於理解今天的例子,都會很有協助的。 在php中,產生驗證碼的步驟: 產生隨機數 -- 建立圖片 -- 隨機數寫成圖片 --儲存在session中。 一,輸入驗證碼gdchek.php
- /*
- * 產生圖片驗證碼
- * and open the template in the editor.
- */
- session_start();
- for($i=0;$i<4;$i++){
- $rand.=dechex(rand(1,15)); //產生4位元包含十六進位的隨機數
- }
- $_SESSION[check_gd]=$rand;
- $img=imagecreatetruecolor(100,30); //建立圖片
- $bg=imagecolorallocate($img,0,0,0); //第一次產生的是背景顏色
- $fc=imagecolorallocate($img,255,255,255); //產生的字型顏色
- //給圖片畫線
- for($i=0;$i<3;$i++){
- $te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
- imageline($img,rand(0,15),0,100,30,$te);
- }
- //給圖片畫點
- for($i=0;$i<200;$i++){
- $te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
- imagesetpixel($img,rand()%100,rand()%30,$te);
- }
- //首先要將文字轉換成utf-8格式
- //$str=iconv("gb2312","utf-8","呵呵呵");
- //加入中文的驗證 bbs.it-home.org
- //smkai.ttf是一個字型檔,為了在別人的電腦中也能起到字型作用,把檔案放到項目的根目錄,可以下載,還有本機C:\WINDOWS\Fonts中有
- imagettftext($img,11,10,20,20,$fc,"simkai.ttf","你好你好");
- //把字串寫在圖片中
- //imagestring($img,rand(1,6),rand(3,70),rand(3,16),$rand,$fc);
- //輸出圖片
- header("Content-type:image/jpeg");
- imagejpeg($img);
- ?>
複製代碼2,登入頁面 login.php
- /*
- * 驗證登入,用到了驗證碼
- */
- session_start();
- if($_POST[sub]){
- //判斷驗證碼是否相同
- if($_POST[gd_pic]==$_SESSION[check_gd]){
- echo "驗證成功!";
- }else{
- echo "驗證碼錯誤";
- }
- }
- ?>
複製代碼 |