简述php设计验证码的过程
php  /  管理员 发布于 7年前   369
简述php设计验证码的过程
php使用GD库生成验证码
一、绘图步骤:
1.创建画布,分配颜色,使用以下两个函数(可以在php手册GD库函数中找到):
imagecreatetruecolor()
imagecolorallocate()
2.绘画过程:
imagefill()
imagettftext()
imagesetpixel()
imageline()
3.输出图像:
header(“Content-Type:image/png”);
imagepng();
imagejpeg();
4.销毁图片(释放内存)
imagedestroy();
二、绘图具体步骤
生成验证码字符串
private function getCode(){ $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $t=''; for($i=0;$i<$this->m;$i++){$t.=$str[rand(0,$this->type['letter'])]; } return $t; }
画出验证码
function drawCode(){ $code=$this->getCode();//获取验证码字符串 imagefill($this->im, 0, 0, $this->bg); for ($i=0; $i <200 ; $i++) { imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255)); } imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255)); $c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200)); for($i=0;$i<$this->m;$i++){imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] ); } }
输出图像
function printImage(){ $this->drawCode(); header("Content-Type:image/jpeg"); imagepng($this->im); $this->destroy(); } function destroy(){ imagedestroy($this->im); }
三、代码演示
<?php/*** 验证码画图类*/class Image { private $width; private $height; private $im; private $bg; private $m; private $type=array('num' => 9,'letter' => 61 ); function __construct($width,$height,$m) { $this->width=$width; $this->height=$height; $this->m=$m; $this->im=imagecreatetruecolor($this->width, $this->height); $this->bg=imagecolorallocate($this->im, 220, 220, 220); } private function getCode(){ $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $t=''; for($i=0;$i<$this->m;$i++){$t.=$str[rand(0,$this->type['letter'])]; } return $t; } function drawCode(){ $code=$this->getCode();//获取验证码字符串 imagefill($this->im, 0, 0, $this->bg); //加干扰点 for ($i=0; $i <200 ; $i++) { imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255)); } //加干扰线 imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255)); $c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200)); for($i=0;$i<$this->m;$i++){imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] ); } } function printImage(){ $this->drawCode(); header("Content-Type:image/jpeg"); imagepng($this->im); $this->destroy(); } function destroy(){ imagedestroy($this->im); }}function unit_test(){ $obj=new Image(20*4,30,4); $obj->printImage();}unit_test();?>
更多PHP相关知识,请访问!
以上就是简述php设计验证码的过程的详细内容,更多请关注其它相关文章!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号