PHP 绘制图形验证码
// 设定验证码中可能出现的文字 $str = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $result = ''; // 取随机4位做验证码 for($i = 0; $i < 4; $i ++) { $result.= $str[mt_rand(0, strlen($str) - 1)]; } // 创建画布,设置背景色 $img = imagecreatetruecolor(100, 30); $background = imagecolorallocate($img, 255, 255, 255); imagefill($img,0,0,$background); // 填充验证码文字 for($i = 0; $i < strlen($result); $i ++) { $te = imagecolorallocate($img, mt_rand(0, 150),mt_rand(0, 150),mt_rand(0, 150)); imagettftext($img, 26, mt_rand(2, 10), $i * 21 + 10,26, $te, "./CHILLER.TTF", $result[$i]); } // 绘制干扰线条 for($i = 0; $i < 3; $i ++) { $te = imagecolorallocate($img, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150)); imageline($img, 0, mt_rand(0, 30), mt_rand(40,100), mt_rand(0, 50), $te); } // 绘制干扰噪点 for($i = 0; $i < 200; $i ++) { $t = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); imagesetpixel($img, mt_rand(1, 100), mt_rand(1, 30), $t); } // 输出图片 header("Content-type: image/jpeg"); imagejpeg($img); die();
欢迎转载,转载时请注明来源。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?