搜尋
首頁後端開發php教程php圖片驗證碼代碼多個實例

  1. //檔案頭...

  2. header("Content-type: image/png");
  3. //建立真彩色白紙
  4. $im = @imagecreatetruecolor(50, 20) or die("建立圖片失敗");
  5. //取得背景顏色
  6. $background_color = imagecolorallocate($ im, 255, 255, 255);
  7. //填滿背景顏色(這個東西類似油桶)
  8. imagefill($im,0,0,$background_color);
  9. //取得邊框顏色
  10. $border_color = imagecolorallocate($im,200,200,200);
  11. //畫出矩形,邊框顏色200,200,200
  12. imagerectangle($im,0,0,49,19,$border_color);>

    //逐行炫耀背景,全螢幕用1或0

  13. for($i=2;$i //取得隨機淡色
  14. $line_color = imagecolorallocate( $im,rand(200,255),rand(200,255),rand(200,255));
  15. //畫線
  16. imageline($im,2,$i,47,$i,$line_color);
  17. }
  18. //設定字體大小

  19. $font_size=12;
  20. //設定印上去的文字

  21. $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  22. $Str[1] = "abcdefghijklmnopqrstuvwxyz";
  23. $Str[2] = "01234567891234567890123456" 🎜> $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];
  24. $imstr[0]["x"] = rand(2, 5);
  25. $imstr[0]["y"] = rand(1,4);
  26. //取得第2個隨機文字

  27. $imstr[1] ["s"] = $Str[rand(0,2)][rand(0,25)];
  28. $imstr[1]["x"] = $imstr[0]["x"] $ font_size-1 rand(0,1);
  29. $imstr[1]["y"] = rand(1,3);
  30. //取得第3個隨機文字

  31. $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];
  32. $imstr[2]["x"] = $imstr[1 ]["x"] $font_size-1 rand(0,1);
  33. $imstr[2]["y"] = rand(1,4);
  34. //取得第4個隨機文字

  35. $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
  36. $imstr[3]["x "] = $imstr[2]["x"] $font_size-1 rand(0,1);
  37. $imstr[3]["y"] = rand(1,3);
  38. //寫入隨機字串

  39. for($i=0;$i //取得隨機較深顏色
  40. $text_color = imagecolorallocate($ im,rand(50,180),rand(50,180),rand(50,180));
  41. //畫文字
  42. imagechar($im,$font_size,$imstr[$i]["x"],$imstr [$i]["y"],$imstr[$i]["s"],$text_color);
  43. }
  44. //顯示圖片

  45. imagepng($ im);
  46. //銷毀圖片
  47. imagedestroy($im);
  48. ?>
  49. 複製程式碼
複製程式碼

例2,漂亮的PHP圖片驗證碼實例 完整程式碼:

  1. /*
  2. * @Author fy
  3. */
  4. $imgwidth =100; //圖片寬度
  5. */
  6. $imgwidth =100; //圖片寬度
  7. 40; //圖片高度
  8. $codelen =4; //驗證碼長度
  9. $fontsize =20; //字體大小
  10. $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';$im=imagecreatetruecolor($imgwidth,$imgheight);
  11. $while=imageColorAllocate($im,255,255,255);
  12. imagefill($im,00,000 //填滿圖片
  13. //取得字串
  14. $authstr='';
  15. $_len = strlen($charset)-1;
  16. for ($i=0;$i $authstr .= $charset[mt_rand(0,$_len)];
  17. }
  18. session_start();
  19. $_SESSION['scode']=strtolower($authstr );//全部轉為小寫,主要是為了不區分大小寫
  20. //隨機畫點,已經改為劃星星了
  21. for ($i=0;$i $randcolor=imageColorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  22. imagestring($im,mt_rand(1,5), mtm(> imagestring($im,mt_rand(1,5), mtgid(0,$imgid0,$img) ,mt_rand(0,$imgheight), '*',$randcolor);
  23. //imagesetpixel($im,mt_rand(0,$imgwidth),mt_rand(0,$imgheight),$randcolor);
  24. }
  25. //隨機畫線,線條數量=字元數量(隨便)
  26. for($i=0;$i{
  27. $randcolor=imagecolorallocate($ im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  28. imageline($im,0,mt_rand(0,$imgheight),$imgwidth,mt_rand(0,$imgheight),$rand) ;
  29. }
  30. $_x=intval($imgwidth/$codelen); //計算字元距離
  31. $_y=intval($imgheight*0.7); //字元顯示在圖片70%的位置
  32. for($i=0;$i $randcolor=imagecolorallocate($im,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
  33. //imagestring($im,5,$j,5,$imgstr[$i],$color3);
  34. // imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
  35. imagettftext($im,$fontsize,mt_rand(-30,30),$i*$_x 3,$_y,$randcolor,$font ,$authstr[$i]);
  36. }
  37. //產生圖片
  38. header("content-type:image/PNG");
  39. imagePNG($im);
imageDestroy( $im);
複製程式碼

例3,php5 圖片驗證碼實例碼

php5產生圖片驗證碼的範例。

需要用到php GD函式庫函數: 1,imagecreatetruecolor -----創建一個真彩色的圖像 imagecreatetruecolor(int x_size,int y_size) //x表示寬,y表示高 2,imagecolorallocate 為一幅圖像分配顏色(調色板) imagecolorallocate(resource image,int red,int green,int blue)//red,green,blue----三原色 3,imagestring 繪圖函數 iamgestring(resource image,font,int x,int y,內容,顏色); 4,輸出函數 php的header是定義頭的動作,php5中支援3中類型: 1,Content-type:xxxx/yyyy 2,Location:xxxx:yyyy/zzzz 3,Status:nnn xxxxxx xxxx/yyyy表示內容檔案的類型 如:image/gif image/jpeg image/png 範例: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(十進制數) //轉換為十六進位

產生驗證碼的步驟: 產生隨機數 -- 建立圖片 -- 隨機數寫成圖片 --儲存在session中

輸入驗證碼例子 gdchek.php

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

login.php

  1. /*
  2. *
  3. *
  4. */
  5. session_start();
  6. if ($_POST[sub]){
  7. //判斷驗證碼是否相同
  8. if($_POST[gd_pic]==$_SESSION[check_gd]){
  9. echo "驗證成功!";
  10. }else{
  11. echo "驗證碼錯誤";
  12. }
  13. }
  14. ?>
  15. 使用者名稱:
  16. 密碼:
  17. 驗證碼:php圖片驗證碼代碼多個實例
複製程式碼


陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
在Laravel中使用Flash會話數據在Laravel中使用Flash會話數據Mar 12, 2025 pm 05:08 PM

Laravel使用其直觀的閃存方法簡化了處理臨時會話數據。這非常適合在您的應用程序中顯示簡短的消息,警報或通知。 默認情況下,數據僅針對後續請求: $請求 -

PHP記錄:PHP日誌分析的最佳實踐PHP記錄:PHP日誌分析的最佳實踐Mar 10, 2025 pm 02:32 PM

PHP日誌記錄對於監視和調試Web應用程序以及捕獲關鍵事件,錯誤和運行時行為至關重要。它為系統性能提供了寶貴的見解,有助於識別問題並支持更快的故障排除

php中的捲曲:如何在REST API中使用PHP捲曲擴展php中的捲曲:如何在REST API中使用PHP捲曲擴展Mar 14, 2025 am 11:42 AM

PHP客戶端URL(curl)擴展是開發人員的強大工具,可以與遠程服務器和REST API無縫交互。通過利用Libcurl(備受尊敬的多協議文件傳輸庫),PHP curl促進了有效的執行

簡化的HTTP響應在Laravel測試中模擬了簡化的HTTP響應在Laravel測試中模擬了Mar 12, 2025 pm 05:09 PM

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显著减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

在Codecanyon上的12個最佳PHP聊天腳本在Codecanyon上的12個最佳PHP聊天腳本Mar 13, 2025 pm 12:08 PM

您是否想為客戶最緊迫的問題提供實時的即時解決方案? 實時聊天使您可以與客戶進行實時對話,並立即解決他們的問題。它允許您為您的自定義提供更快的服務

解釋PHP中晚期靜態結合的概念。解釋PHP中晚期靜態結合的概念。Mar 21, 2025 pm 01:33 PM

文章討論了PHP 5.3中介紹的PHP中的晚期靜態結合(LSB),允許靜態方法的運行時間分辨率調用以更靈活的繼承。 LSB的實用應用和潛在的觸摸

自定義/擴展框架:如何添加自定義功能。自定義/擴展框架:如何添加自定義功能。Mar 28, 2025 pm 05:12 PM

本文討論了將自定義功能添加到框架上,專注於理解體系結構,識別擴展點以及集成和調試的最佳實踐。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。