• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

利用php生成验证码

php 搞代码 4年前 (2022-01-01) 18次浏览 已收录 0个评论

本文主要分享了利用php生成验证码的示例代码,具有很好的参考价值,下面跟着小编一起来看下吧

话不多说,请看代码:

 <?php /** * php生成验证码 * @param $width 画布宽 * @param $height 画布高 * @param $vcodelen 验证码长度 * @param $pointnum 干扰像素点数量 * @param $linenum 干扰线条数量 * * 思路:创建验证码画布,生成并填充背景色,生成验证码内容/干扰像素点/线,填充到画布,输出。 */ $width = 100; $height = 30; $vcodelen = 4; $pointnum = 200; $linenum = 3; // 创建画布 $image = imagecreatetruecolor($width, $height); // 创建色块 $bgcolor = imagecolorallocate($image, 255, 255, 255); // 填充画布背景色 imagefill($image, 0, 0, $bgcolor); // 验证码内容 for ($i=0; $i < $vcodelen; $i++) { // 字体大小 $fontsize = 5; // 字体颜色,颜色在限定范围内随机 $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(<i style="color:transparent">来源gaodai$ma#com搞$$代**码网</i>0,120)); $data = 'abcdefghijklmnopqrstuvwxyz0123456789'; // 验证码内容在以上字符串内随机截取 $fontcontent = substr($data, rand(0,strlen($data)),1); // 字符串显示位置 $x = ($i*$width/4)+rand(5,15); $y = rand(5,10); // 字符串填充图片 // imagestring的字体大小可选1-5,字体再大需要用imagettftext函数(需要字体文件) imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); // imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, '/font/Geneva.dfont', $fontcontent); } // 干扰像素点 for ($i=0; $i < $pointnum; $i++) { $pointcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); // 画布填充像素点函数 imagesetpixel($image, rand(0,$width), rand(0,$height), $pointcolor); } // 干扰线条 for ($i=0; $i 

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持gaodaima搞代码网

以上就是利用php生成验证码的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:利用php生成验证码
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址