最新最全PHP生成制作验证码代码详解(推荐),验证码详解
1.0 首先先看代码
<?php header("Content-Type:text/html;Charset=UTF-");// 设置页面的编码风格 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像 $img = imagecreatetruecolor(,);//创建画布并设置大小 x轴 y轴 $bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景颜色 imagefill($img, , , $bgcolor); ////把背景填充到图像 imagejpeg($img); // 输出图像 imagedestroy($img); // 销毁图像 ?>
好,现在结合以上代码,来分析分析以上用到的几个函数:
① imagecreatetruecolor();
imagecreatetruecolor — 新建一个真彩色图像(感觉哇,那么长,其实仔细一看挺好记的 image/create/true/color,什么是真彩色图像?往下看)
resource imagecreatetruecolor ( int $width , int $height )
imagecreatetruecolor() 和 imagecreate()两个函数都能创建画布
resource imagecreate ( int $x_size , int $y_size )
imagecreatetruecolor()建立的是一幅大小为 x和 y的黑色图像(默认为黑色[即便叫法就是真彩色图像]),如想改变背景颜色则需
要用填充颜色函数 imagefill($img,0,0,$color);
imagecreate 新建一个空白图像资源,用imagecolorAllocate()添加背景色
上面两个函数只不过是一个功能的两种方法
② imagecolorallocate();
imagecolorallocate — 为一幅图像分配颜色
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
颜色分别用 红 绿 蓝三色组合,这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。
③ mt_rand();
mt_rand — 生成更好的随机数
int mt_rand ( int $min , int $max )
$min 可选的、返回的最小值(默认:0) $max 可选的、返回的最大值(默认:mt_getrandmax())
这里就是用来让他随机生成背景颜色,0-255随便取值。所以页面没刷新一次画布背景颜色就不一样。效果图:
2.0 开始往里面做干扰线,干扰点。防止验证图像被秒识别
<?php header("Content-Type:text/html;Charset=UTF-");// 设置页面的编码风格 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像 $img = imagecreatetruecolor(,);//创建画布并设置大小 x轴 y轴 $bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景颜色 //添加干扰线,并循环次,背景颜色随机 for($i=;$i<;$i++){ $linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,)); imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor); } //添加干扰点,并循环次,背景颜色随机 for($i=;$i<;$i++){ $dotcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,)); imagesetpixel($img, mt_rand(,), mt_rand(,), $dotcolor); } imagefill($img, , , $bgcolor); ////把背景填充到图像 imagejpeg($img); // 输出图像 imagedestroy($img); // 销毁图像 ?>
函数分析:
① imageline();
imageline — 画一条线段
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);这里意思就是 画布$img 中从坐标 x1,y1 到 x2,y2随机
② imagesetpixel();
imagesetpixel— 画一个单一像素
bool imagesetpixel ( resource $image , int $x , int $y , int $color )
imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);具体含义同上
效果图:
3.0 添加验证字母数字
<?php header("Content-Type:text/html;Charset=UTF-");// 设置页面的编码风格 header("Content-Type:image/jpeg");// 通知浏览器输出的是jpeg格式的图像 $img = imagecreatetruecolor(,);//创建画布并设置大小 x轴 y轴 $bgcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,));//分配背景颜色 //添加干扰线,并循环次,背景颜色随机 for($i=;$i<;$i++){ $linecolor = imagecolorallocate($img,mt_rand(,),mt_rand(,),mt_rand(,)); imageline($img, mt_rand(,), mt_rand(,), mt_rand(,), mt_rand(,), $linecolor); } //添加干扰点,并循环次,背景颜色随机 for($i=;$i<;$i++){ $dotcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,)); imagesetpixel($img, mt_rand(,), mt_rand(,), $dotcolor); } //添加需要验证的字母或者数字 $rand_str = "qwertyuiopasdfghjklzxcvbnm";//需要使用到验证的一些字母和数字 $str_arr = array(); //命名一个数组 for($i = ;$i<;$i++){ //循环次,就是有四个随机的字母或者数字 $pos = mt_rand(,strlen($rand_str)-); $str_arr[] = $rand_str[$pos];//临时交换 } $x_start=/;//单个字符X轴位置 foreach ($str_arr as $key) { $fontcolor = imagecolorallocate($img, mt_rand(,), mt_rand(,), mt_rand(,)); imagettftext($img, , mt_rand(-,), $x_start, /, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key); $x_start +=;//遍历后单个字符沿X轴 + } imagefill($img, , , $bgcolor); ////把背景填充到图像 imagejpeg($img); // 输出图像 imagedestroy($img); // 销毁图像 ?>
函数:
imagettftext();
imagettftext — 用 TrueType 字体向图像写入文本
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
分析下面的代码:
imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);
$img-----------画布
25-----------字体的尺寸。
mt_rand(-15,15)----------角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。(就是字体角度的问题,)
$x_start----------通俗易懂的讲就是字符的X轴位置
50/2----------字符的高度
$fontcolor----------字符颜色
"C:/Windows/Fonts/Verdana.TTF"----------字符的字体样式路径
$key-----------遍历出后的字符
效果:
以上内容是本文给大家介绍的最新最全PHP生成制作验证码代码详解(推荐)的全部叙述,希望对大家有所帮助!

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。

在PHP中,trait適用於需要方法復用但不適合使用繼承的情況。 1)trait允許在類中復用方法,避免多重繼承複雜性。 2)使用trait時需注意方法衝突,可通過insteadof和as關鍵字解決。 3)應避免過度使用trait,保持其單一職責,以優化性能和提高代碼可維護性。

依賴注入容器(DIC)是一種管理和提供對象依賴關係的工具,用於PHP項目中。 DIC的主要好處包括:1.解耦,使組件獨立,代碼易維護和測試;2.靈活性,易替換或修改依賴關係;3.可測試性,方便注入mock對象進行單元測試。

SplFixedArray在PHP中是一種固定大小的數組,適用於需要高性能和低內存使用量的場景。 1)它在創建時需指定大小,避免動態調整帶來的開銷。 2)基於C語言數組,直接操作內存,訪問速度快。 3)適合大規模數據處理和內存敏感環境,但需謹慎使用,因其大小固定。

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

JavaScript中處理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。 1.??返回第一個非null或非undefined的操作數。 2.??=將變量賦值為右操作數的值,但前提是該變量為null或undefined。這些操作符簡化了代碼邏輯,提高了可讀性和性能。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6
視覺化網頁開發工具