广告 全新升级

PHP 生成验证码图片

  [复制链接]
查看43459 | 回复1638 | 2021-4-3 16:33 | 显示全部楼层 |阅读模式
  1. /**
  2. * 验证码类
  3. */
  4. class VeriCode
  5. {
  6.     // 获取验证码配置
  7.     private static function _getCodeConfig()
  8.     {
  9.         return [
  10.             // 验证码字符集
  11.             'codeStr' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
  12.             // 验证码个数
  13.             'codeCount' => 4,
  14.             // 字体大小
  15.             'fontsize' => 25,
  16.             // 验证码的宽度
  17.             'width' => 130,
  18.             // 验证码高度
  19.             'height' => 36,
  20.             // 是否有干扰点?true有,false没有
  21.             'disturbPoint' => false,
  22.             // 干扰点个数,disturbPoint开启后生效
  23.             'pointCount' => 1,
  24.             // 是否有干扰条?true有,false没有
  25.             'disturbLine' => false,
  26.             // 干扰条个数,disturbLine开启后生效
  27.             'lineCount' => 9
  28.         ];
  29.     }

  30.     // 创建图片验证码
  31.     public static function create()
  32.     {
  33.         // 配置
  34.         $config = self::_getCodeConfig();

  35.         //创建画布
  36.         $image = imagecreatetruecolor($config['width'], $config['height']);
  37.         //背景颜色
  38.         $bgcolor = imagecolorallocate($image, 255, 255, 255);
  39.         imagefill($image, 0, 0, $bgcolor);
  40.         $captch_code = '';//存储验证码
  41.         $captchCodeArr = str_split($config['codeStr']);

  42.         //随机选取4个候选字符
  43.         for ($i = 0; $i < $config['codeCount']; $i ) {
  44.             $fontsize = $config['fontsize'];
  45.             $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));//随机颜色
  46.             $fontcontent = $captchCodeArr[rand(0, strlen($config['codeStr']) - 1)];
  47.             $captch_code .= $fontcontent;
  48.             $_x = $config['width'] / $config['codeCount'];
  49.             $x = ($i * (int)$_x) rand(5, 10);   //随机坐标
  50.             $y = rand(5, 10);
  51.             imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);    // 水平地画一行字符串

  52.         }

  53.         session_start();
  54.         $_SESSION['code'] = $captch_code;
  55.         //增加干扰点
  56.         if ($config['disturbPoint']) {
  57.             for ($i = 0; $i < $config['pointCount']; $i ) {
  58.                 $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
  59.                 imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor);

  60.             }
  61.         }

  62.         //增加干扰线
  63.         if ($config['disturbLine']) {
  64.             for ($i = 0; $i < $config['lineCount']; $i ) {
  65.                 $linecolor = imagecolorallocate($image, rand(80, 280), rand(80, 220), rand(80, 220));
  66.                 imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1, 29), $linecolor);
  67.             }
  68.         }

  69.         //输出格式
  70.         header('content-type:image/png');
  71.         imagepng($image);

  72.         //销毁图片
  73.         imagedestroy($image);
  74.     }
复制代码
调用
  1. VeriCode::create();
复制代码


蓝奏链接如访问不了把lanzous换成lanzoux或lanozui
;主题回复不显示隐藏内容就刷新页面;
这东西我收了!谢谢楼主!
回复

使用道具 举报

楼主,大恩不言谢了!
回复

使用道具 举报

訆聲爹厛厛 | 2021-4-3 16:33 | 显示全部楼层
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!
回复

使用道具 举报

晓Q宸 | 2021-4-3 16:34 | 显示全部楼层
楼主,我太崇拜你了!
回复

使用道具 举报

社区不能没有像楼主这样的人才啊!
回复

使用道具 举报

i我非良人r | 2021-4-3 16:34 | 显示全部楼层
楼主发贴辛苦了,谢谢楼主分享!
回复

使用道具 举报

__深海未蓝 | 2021-4-3 16:34 | 显示全部楼层
其实我一直觉得楼主的品味不错!呵呵!
回复

使用道具 举报

訆聲爹厛厛 | 2021-4-3 16:34 | 显示全部楼层
我看不错噢 谢谢楼主!
回复

使用道具 举报

感谢楼主的无私分享!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则