php验证码的制作思路和实现方法
2019/6/30 15:58:12
本文主要是介绍php验证码的制作思路和实现方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、制作思路
由于注册的时候常常会用到注册码来防止机器恶意注册,这里我发表一个产生png图片验证码的基本图像,简单的思路分析:
1、产生一张png的图片
2、为图片设置背景色
3、设置字体颜色和样式
4、产生4位数的随机的验证码
5、把产生的每个字符调整旋转角度和位置画到png图片上
6、加入噪点和干扰线防止注册机器分析原图片来恶意注册
7、输出图片
8、释放图片所占内存
二、实现方法
authcode.php文件
<?php session_start (); header ( 'Content-type: image/png' ); //创建图片 $im = imagecreate($x=130,$y=45 ); $bg = imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155)); //第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色 $fontColor = imageColorAllocate ( $im, 255, 255, 255 ); //字体颜色 $fontstyle = 'rock.ttf'; //字体样式,这个可以从c:\windows\Fonts\文件夹下找到,我把它放到和authcode.php文件同一个目录,这里可以替换其他的字体样式 //产生随机字符 for($i = 0; $i < 4; $i ++) { $randAsciiNumArray = array (rand(48,57),rand(65,90)); $randAsciiNum = $randAsciiNumArray [rand ( 0, 1 )]; $randStr = chr ( $randAsciiNum ); imagettftext($im,30,rand(0,20)-rand(0,25),5+$i*30,rand(30,35),$fontColor,$fontstyle,$randStr); $authcode .= $randStr; } $_SESSION['authcode'] = $randFourStr;//用户和用户输入的验证码做比较 //干扰线 for ($i=0;$i<8;$i++){ $lineColor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imageline ($im,rand(0,$x),0,rand(0,$x),$y,$lineColor); } //干扰点 for ($i=0;$i<250;$i++){ imagesetpixel($im,rand(0,$x),rand(0,$y),$fontColor); } imagepng($im); imagedestroy($im); ?>
效果图:
以上就是php验证码的制作思路和实现方法,希望对大家的学习有所帮助。
这篇关于php验证码的制作思路和实现方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28怎么把PHP程序打包?-icode9专业技术文章分享
- 2024-09-28怎么用Phar打包PHP程序?-icode9专业技术文章分享
- 2024-09-13手动在github上下载的mfpt包,怎么放到thinkphp5.0框架并正常使用-icode9专业技术文章分享
- 2024-09-05python的<class 'bytearray'>相当于php的哪个数据类型-icode9专业技术文章分享
- 2024-09-05php 导出银行卡号避免科学技术法的方法-icode9专业技术文章分享
- 2024-08-30什么样的php代码质量差被称为垃圾代码-icode9专业技术文章分享
- 2024-08-30用 PHP 调用拼多多的接口以获取订单状态消息的步骤方法和代码示例-icode9专业技术文章分享
- 2024-08-27phpunit单元测试框架的入门和使用方法介绍-icode9专业技术文章分享
- 2024-08-24PHP 中date("w") 周一是多少-icode9专业技术文章分享
- 2024-08-14thinkphp8.0获取域名或主机名方法-icode9专业技术文章分享