温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在php中生成验证码图片

发布时间:2021-03-18 09:58:34 来源:亿速云 阅读:111 作者:小新 栏目:编程语言

小编给大家分享一下怎么在php中生成验证码图片,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

1.获取生成验证码字体:

在php文件路径新建一个fonts文件夹,里面有字体文件。

     //判定字体资源
      if(empty($fonts))
         $fonts="arial.ttf";

      //确认字体路径
      $fonts=__DIR__."/fonts/".$fonts;
      $fonts=str_replace("/","\\",$fonts);

2.制作画布,随机分配背景色

$img=imagecreatetruecolor($width,$height);
      
$bg_color=imagecolordeallocate($img,mt_rand(200,255),mt_rand(200,250));
imagefilter($img,0,0,$bg_color);

3.增加干扰点、线

//增加干扰点:*
      for($i = 0;$i < 50;$i++)
      {
      //随机颜色
       $dots_color = imagecolorallocate($img, mt_rand(140,190), mt_rand(140,190), mt_rand(140,190));
      //使用*号作为干扰点
       imagestring($img, mt_rand(1,5), mt_rand(0,$width), mt_rand(0,$height), '*', $dots_color);
      }
     //增加干扰线
     for($j = 0;$j < 10;$j++)
     {
      //随机线段颜色
      $line_color = imagecolorallocate($img, mt_rand(80,130), mt_rand(80,130), mt_rand(80,130));
      //随机线段
      imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$line_color);
     }

4.将验证码放入图片

 $captcha=array(3,4,'a','i');//可以自己使用写一个方法生成数组;
 $length=sizeof($captcha);
   for($i = 0;$i < $length;$i++){
   //给每个字符分配不同颜色
   $c_color = imagecolorallocate($img, mt_rand(0,60), mt_rand(0,60), mt_rand(0,60));
   
   //增加字体空间、大小、角度显示
imagettftext($img,mt_rand(15,25),mt_rand(-45,45),$width/($length+1)*($i+1),mt_rand(25,$height-25),$c_color,$fonts,$captcha[$i]);
  }

5.保存图片

imagejpeg($img,"test.jpg",100);

以上是“怎么在php中生成验证码图片”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI