验证码
1 <?php 2 // 创建画布 3 $img = imagecreatetruecolor(170, 40); 4 5 // 创建背景颜色 6 $backcolor = imagecolorallocate($img,mt_rand(200,255), mt_rand(200,255), mt_rand(200,255)); 7 // 填充背景 8 imagefill($img, 0, 0, $backcolor); 9 10 // 定义数组 11 $arr = array_merge(range('a','z'), range('A','Z'), range(0, 9)); 12 13 // 打乱数组 14 shuffle($arr); 15 16 // 随机抽取数组的下标值 17 $rand_keys = array_rand($arr,4); 18 19 // 遍历获得原数组的值 20 $str = ''; 21 foreach($rand_keys as $value){ 22 $str .= $arr[$value]; 23 } 24 25 // 将验证码字符写到图片上 26 // 计算字符间距 27 $span = ceil(170/(4+1)); 28 29 // 将验证码字符写在图片上 30 for($i=1;$i<=4;$i++){ 31 $stringColor = imagecolorallocate($img, mt_rand(0,150), mt_rand(0,150), mt_rand(0,150)); 32 // 绘制文字 33 imagestring($img, 5, $i*$span, 10, $str[$i-1], $stringColor); 34 } 35 36 // 添加干扰线 37 for($i=1;$i<=9;$i++){ 38 $lineColor = imagecolorallocate($img, mt_rand(100,200), mt_rand(100,200),mt_rand(100,200)); 39 imageline($img,mt_rand(0,169),mt_rand(0,39),mt_rand(0,169),mt_rand(0,39),$lineColor); 40 } 41 42 // 添加干扰点 43 for($i=1;$i<=170*40*0.05;$i++){ 44 $pixelColor = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); 45 imagesetpixel($img, mt_rand(0,169), mt_rand(0,39), $pixelColor); 46 } 47 48 // 输出图片 49 header("content-type:image/png"); 50 51 // 清理数据缓冲区 52 imagepng($img); 53 54 // 保存图片 55 imagepng($img,'./hello.png');
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· Supergateway:MCP服务器的远程调试与集成工具
· C# 13 中的新增功能实操