php5 圖片驗證碼的執行個體代碼

來源:互聯網
上載者:User
  1. 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

  1. /*
  2. * 產生圖片驗證碼
  3. * and open the template in the editor.
  4. */
  5. session_start();
  6. for($i=0;$i<4;$i++){
  7. $rand.=dechex(rand(1,15)); //產生4位元包含十六進位的隨機數
  8. }
  9. $_SESSION[check_gd]=$rand;
  10. $img=imagecreatetruecolor(100,30); //建立圖片
  11. $bg=imagecolorallocate($img,0,0,0); //第一次產生的是背景顏色
  12. $fc=imagecolorallocate($img,255,255,255); //產生的字型顏色
  13. //給圖片畫線
  14. for($i=0;$i<3;$i++){
  15. $te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  16. imageline($img,rand(0,15),0,100,30,$te);
  17. }
  18. //給圖片畫點
  19. for($i=0;$i<200;$i++){
  20. $te=imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  21. imagesetpixel($img,rand()%100,rand()%30,$te);
  22. }
  23. //首先要將文字轉換成utf-8格式
  24. //$str=iconv("gb2312","utf-8","呵呵呵");
  25. //加入中文的驗證 bbs.it-home.org
  26. //smkai.ttf是一個字型檔,為了在別人的電腦中也能起到字型作用,把檔案放到項目的根目錄,可以下載,還有本機C:\WINDOWS\Fonts中有
  27. imagettftext($img,11,10,20,20,$fc,"simkai.ttf","你好你好");
  28. //把字串寫在圖片中
  29. //imagestring($img,rand(1,6),rand(3,70),rand(3,16),$rand,$fc);
  30. //輸出圖片
  31. header("Content-type:image/jpeg");
  32. imagejpeg($img);
  33. ?>
複製代碼

2,登入頁面 login.php

  1. /*
  2. * 驗證登入,用到了驗證碼
  3. */
  4. session_start();
  5. if($_POST[sub]){
  6. //判斷驗證碼是否相同
  7. if($_POST[gd_pic]==$_SESSION[check_gd]){
  8. echo "驗證成功!";
  9. }else{
  10. echo "驗證碼錯誤";
  11. }
  12. }
  13. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.