• 验证码


     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');
  • 相关阅读:
    浏览器返回按钮不会触发onLoad事件
    TCP慢启动算法
    TCP协议三次握手过程分析
    关于新增和编辑
    Mock, 让你的开发脱离接口
    到底数据驱动是个什么玩意
    pagination分页插件
    关于状态切换
    在线占位图网站
    Arduino nano 与 w5500模块的连接与调试
  • 原文地址:https://www.cnblogs.com/php08049/p/7634455.html
Copyright © 2020-2023  润新知