\(^o^)/~
现在网上越来越离不开验证码了,不知道小伙伴们知不知利用php的GD库就可以生成验证码,Σ(⊙▽⊙"a ......
首先介绍几个需要用的函数。
1.imagesetpixel()
这个函数可以进行像素点的绘制,在验证码中,我们称之为“噪点”,简直是一个神器。不知道小伙伴有没有想起来验证码上的点点呢,就是用这个函数生成的。
2.str_shuffle()
利用这个打乱字符串,然后利用substr()截取给定的位数,就可以生成一个随机字符串啦。
实例:
1 <?php 2 3 $img = imagecreatetruecolor(100,50); 4 $black = imagecolorallocate($img,1)">0x00,1)">0x00 5 $green = imagecolorallocate($img,1)">0xFF,1)"> 6 $white = imagecolorallocate($img,1)">0xFF 7 imagefill($img,1)">0,1)">0,$white); 8 //生成随机的验证码 9 $code = make(510 imagestring($img,1)">5,1)">10,1)">1011 加入噪点干扰 12 for ($i = 0; $i < 300; $i++) { 13 imagesetpixel($img,rand(100),1)">100),1)">14 imagesetpixel($img,$green); 15 } 16 加入线段干扰 17 for ($n = 0; $n <= 1; $n++18 imageline($img,1)">40),1)">4019 imageline($img,1)">20 21 输出验证码 22 header("content-type: image/png"23 imagepng($img); 24 销毁图片 25 imagedestroy($img); 26 生产随机验证码的函数 27 function make($length) 28 { 29 $code = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 30 return substr(str_shuffle($code),$length); 31 }
效果: