PHP 验证码如何生成类完整代码

合集下载

PHP如何实现验证码

PHP如何实现验证码

PHP如何实现验证码现在来说说简单的纯数字验证码吧。

如果是初学者,建议按照我代码的注释 //数字⼀步步来。

最简单的⽅法,还是把整个代码复制⾛了。

新建⼀个captcha.php:<?php//11>设置session,必须处于脚本最顶部session_start();/*$image = imagecreatetruecolor(100, 30); //1>设置验证码图⽚⼤⼩的函数//5>设置验证码颜⾊ imagecolorallocate(int im, int red, int green, int blue);$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff//6>区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着⾊,col 表⽰欲涂上的颜⾊imagefill($image, 0, 0, $bgcolor);//10>设置变量$captcha_code = "";*///7>⽣成随机数字for($i=0;$i<4;$i++){//设置字体⼤⼩$fontsize = 6;//设置字体颜⾊,随机颜⾊$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜⾊//设置数字$fontcontent = rand(0,9);//10>.=连续定义变量$captcha_code .= $fontcontent;//设置坐标$x = ($i*100/4)+rand(5,10);$y = rand(5,10);imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);}//10>存到session$_SESSION['authcode'] = $captcha_code;//8>增加⼲扰元素,设置雪花点for($i=0;$i<200;$i++){//设置点的颜⾊,50-200颜⾊⽐数字浅,不⼲扰阅读$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));//imagesetpixel — 画⼀个单⼀像素imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);}//9>增加⼲扰元素,设置横线for($i=0;$i<4;$i++){//设置线的颜⾊$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));//设置线,两点⼀线imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);}//2>设置头部,image/pngheader('Content-Type: image/png');//3>imagepng() 建⽴png图形函数imagepng($image);//4>imagedestroy() 结束图形函数销毁$imageimagedestroy($image);接着就是静态页的代码了:index.html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>⽆标题⽂档</title></head><body><form method="post" action="./form.php"><p>验证码: <img id="captcha_img" border='1' src='./captcha.php?r=echo rand(); ?>' style="width:100px; height:30px" /><a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()">换⼀个?</a> </p><P>请输⼊验证码:<input type="text" name='authcode' value=''/></p><p><input type='submit' value='提交' style='padding:6px 5px;'/></p></form></body></html>从index.html可以看到,提交的表单是到form.php的,所以还要有⼀个判断的form.php代码:<?phpheader("Content-Type:text/html;charset=utf-8"); //设置头部信息//isset()检测变量是否设置if(isset($_REQUEST['authcode'])){session_start();//strtolower()⼩写函数if(strtolower($_REQUEST['authcode'])== $_SESSION['authcode']){//跳转页⾯echo "<script language=\"javascript\">";echo "document.location=\"./form.php\"";echo "</script>";}else{//提⽰以及跳转页⾯echo "<script language=\"javascript\">";echo "alert('输⼊错误!');";echo "document.location=\"./form.php\"";echo "</script>";}exit();}显⽰页⾯如下:数字加英⽂的验证码,只需更改captcha.php页⾯中的 7》即可,其他两个页⾯不需要动,代码如下:<?php//11>设置session,必须处于脚本最顶部session_start();$image = imagecreatetruecolor(100, 30); //1>设置验证码图⽚⼤⼩的函数//5>设置验证码颜⾊ imagecolorallocate(int im, int red, int green, int blue);$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff//6>区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着⾊,col 表⽰欲涂上的颜⾊imagefill($image, 0, 0, $bgcolor);//10>设置变量$captcha_code = "";//7>⽣成随机的字母和数字for($i=0;$i<4;$i++){//设置字体⼤⼩$fontsize = 8;//设置字体颜⾊,随机颜⾊$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜⾊//设置需要随机取的值,去掉容易出错的值如0和o$data ='abcdefghigkmnpqrstuvwxy3456789';//取出值,字符串截取⽅法 strlen获取字符串长度$fontcontent = substr($data, rand(0,strlen($data)),1);//10>.=连续定义变量$captcha_code .= $fontcontent;//设置坐标$x = ($i*100/4)+rand(5,10);$y = rand(5,10);imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);}//10>存到session$_SESSION['authcode'] = $captcha_code;//8>增加⼲扰元素,设置雪花点for($i=0;$i<200;$i++){//设置点的颜⾊,50-200颜⾊⽐数字浅,不⼲扰阅读$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));//imagesetpixel — 画⼀个单⼀像素imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);}//9>增加⼲扰元素,设置横线for($i=0;$i<4;$i++){//设置线的颜⾊$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));//设置线,两点⼀线imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);}//2>设置头部,image/pngheader('Content-Type: image/png');//3>imagepng() 建⽴png图形函数imagepng($image);//4>imagedestroy() 结束图形函数销毁$imageimagedestroy($image);显⽰页⾯如下:⽣成汉字类验证码,在运⾏过程中,提⽰乱码错误,⽆法显⽰,未能解决,代码如下:php//11>设置session,必须处于脚本最顶部session_start();//1>设置验证码图⽚⼤⼩的函数$image = imagecreatetruecolor(200, 60);//5>设置验证码颜⾊ imagecolorallocate(int im, int red, int green, int blue);$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff//6>区域填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的区域着⾊,col 表⽰欲涂上的颜⾊imagefill($image, 0, 0, $bgcolor);//7>设置ttf字体$fontface = 'FZYTK.TTF';//7>设置字库,实现简单的数字储备$str='天地不仁以万物为刍狗圣⼈不仁以百姓为刍狗这句经常出现在控诉暴君暴政上地残暴不仁把万物都当成低贱的猪狗来看待⽽那些⾼⾼在上的所谓圣⼈们也没两样还不是把我们⽼百姓也当成猪狗不如的东西但实在正取的解读是地不情感⽤事对万//str_split()切割字符串为⼀个数组,⼀个中⽂在utf_8为3个字符$strdb = str_split($str,3);//>11$captcha_code = '';//8>⽣成随机的汉⼦for($i=0;$i<4;$i++){//设置字体颜⾊,随机颜⾊$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深颜⾊//随机选取中⽂$in = rand(0,count($strdb));$cn = $strdb[$in];//将中⽂记录到将保存到session的字符串中$captcha_code .= $cn;/*imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color,string $fontfile ,string $text ) 幕布,尺⼨,⾓度,坐标,颜⾊,字体路径,⽂本字符串mt_rand()⽣成更好的随机数,⽐rand()快四倍*/imagettftext($image, mt_rand(20,24),mt_rand(-60,60),(40*$i+20),mt_rand(30,35),$fontcolor,$fontface,$cn);}//11>存到session$_SESSION['authcode'] = $captcha_code;//9>增加⼲扰元素,设置点for($i=0;$i<200;$i++){//设置点的颜⾊,50-200颜⾊⽐数字浅,不⼲扰阅读$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200)); //imagesetpixel — 画⼀个单⼀像素imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor);}//10>增加⼲扰元素,设置线for($i=0;$i<4;$i++){//设置线的颜⾊$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));//设置线,两点⼀线imageline($image,rand(1,199), rand(1,59),rand(1,199), rand(1,59),$linecolor);}//2>设置头部,image/pngheader('Content-Type: image/png');//3>imagepng() 建⽴png图形函数imagepng($image);//4>imagedestroy() 结束图形函数销毁$imageimagedestroy($image);。

PHP验证码

PHP验证码
case 'CHAR':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;
case 'NUMBER':
$chars='0123456789'; break;
imagefill($img, 0, 0, $backcolor);
//画线
imageline($img, mt_rand(0,$intwidth/3), mt_rand(0,$intheight/3), mt_rand($intwidth/3,$intwidth), mt_rand($intheight/3,$intheight), $fontcolor);
$randval .= chr($randstr);//chr()返回相对应于 ascii 所指定的单个字符。
//$randval .= mt_rand(0, 9);
}
}
$_SESSION["randval"] = $randval;
$intheight = 18;//验证码背景图的高
$string="";
while(strlen($string)<$len)
$string.=substr($chars,(mt_rand()%strlen($chars)),1);
return $string;
}
?>
//产生随机字符串
function randStr($len=6,$format='ALL') {
switch($format) {

php验证码实例

php验证码实例

php验证码实例
一、概述
验证码(CAPTCHA)是一种用于验证用户是否为人类的技术,通过向用户显示一些扭曲的字符或完成一些图像识别任务来确保不是机器人提交表单。

在PHP中,我们可以使用GD库来创建自定义的验证码。

本文将介绍一个简单的PHP验证码实例,包括生成验证码图像、验证用户输入和防止验证码被恶意破解的方法。

二、准备工作
在开始之前,请确保你的PHP环境已启用GD库。

你可以通过在PHP配置文件(php.ini)中取消注释以下行来启用GD库:
然后重启Web服务器以使更改生效。

三、验证码生成
下面是一个简单的PHP验证码生成示例代码:
在上述代码中,我们使用GD库创建了一个指定宽度和高度的空白图像,并随机生成了一些扭曲的字符。

我们使用imagettftext函数将字符绘制到图像上,并使用imagepng函数将图像输出为PNG格式。

最后,我们使用imagedestroy函数销毁图像资源。

你可以根据需要调整代码中的参数,例如更改验证码长度、字体样式等。

四、验证码验证与防止恶意破解
在生成验证码后,我们需要验证用户输入的验证码是否正确。

以下是一个简单的验证码验证示例代码:。

php生成4位数字验证码的实现代码

php生成4位数字验证码的实现代码

php⽣成4位数字验证码的实现代码在php中实现验证码还是很⽅便的,关键点在于掌握php gd库与session的⽤法。

纵观⽹上php ⽣成验证码的例⼦,⽆不是php gd库与session相结合,并利⽤php ⽣成随机数的⽅法来完成。

PHP验证码,可以分为很多种,包括 php 图⽚验证码,php 随机验证码,以及php 中⽂验证码等,根据不同的应⽤场合来使⽤不同的验证码。

这⾥分享⼀个php数字验证码,供⼤家参考。

4位数字验证码/**Filename:authpage.php*/session_start();//srand((double)microtime()*1000000);$authnum=$_SESSION['authnum'];//验证⽤户输⼊是否和验证码⼀致if(isset($_POST['authinput'])){if(strcmp($_POST['authinput'],$_SESSION['authnum'])==0)echo"验证成功!";elseecho"验证失败!";}//⽣成新的四位整数验证码//while(($authnum=rand()%10000)<1000);><formaction=test4.phpmethod=post><table>请输⼊验证码:<inputtype=textname=authinputstyle="width:80px"><br><inputtype=submitname="验证"value="提交验证码"><inputtype=hiddenname=authnumvalue=<?echo$authnum;?>><imgsrc=authimg.php?authnum=<?echo$authnum;?>></table></form>authimg.php<?php//⽣成验证码图⽚Header("Content-type:image/PNG");srand((double)microtime()*1000000);//播下⼀个⽣成随机数字的种⼦,以⽅便下⾯随机数⽣成的使⽤session_start();//将随机数存⼊session中$_SESSION['authnum']="";$im=imagecreate(62,20);//制定图⽚背景⼤⼩$black=ImageColorAllocate($im,0,0,0);//设定三种颜⾊$white=ImageColorAllocate($im,255,255,255);$gray=ImageColorAllocate($im,200,200,200);imagefill($im,0,0,$gray);//采⽤区域填充法,设定(0,0)while(($authnum=rand()%100000)<10000);//将四位整数验证码绘⼊图⽚$_SESSION['authnum']=$authnum;imagestring($im,5,10,3,$authnum,$black);//⽤col颜⾊将字符串s画到image所代表的图像的x,y座标处(图像的左上⾓为0,0)。

(完整word版)一个完整的PHP图形验证码程序

(完整word版)一个完整的PHP图形验证码程序

一个完整的PHP图形验证码程序(4个随机数字)PHP代码如下:<?php/* 网站验证码程序* 运行环境: PHP5.0.18 下调试通过* 需要 gd2 图形库支持(PHP.INI中 php_gd2.dll开启)* 文件名: showimg.php*///随机生成一个4位数的数字验证码$num="";for($i=0;$i<4;$i++){$num .= rand(0,9);}//4位验证码也可以用rand(1000,9999)直接生成//将生成的验证码写入session,备验证页面使用Session_start();$_SESSION["Checknum"] = $num;//创建图片,定义颜色值Header("Content-type: image/PNG");srand((double)microtime()*1000000);$im = imagecreate(60,20);$black = ImageColorAllocate($im, 0,0,0);$gray = ImageColorAllocate($im, 200,200,200);imagefill($im,0,0,$gray);//随机绘制两条虚线,起干扰作用$style = array($black, $black, $black, $black, $black, $gray, $ gray, $gray, $gray, $gray);imagesetstyle($im, $style);$y1=rand(0,20);$y2=rand(0,20);$y3=rand(0,20);$y4=rand(0,20);imageline($im, 0, $y1, 60, $y3, IMG_COLOR_STYLED);imageline($im, 0, $y2, 60, $y4, IMG_COLOR_STYLED);//在画布上随机生成大量黑点,起干扰作用;for($i=0;$i<80;$i++){imagesetpixel($im, rand(0,60), rand(0,20), $black);}//将四个数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成 $strx=rand(3,8);for($i=0;$i<4;$i++){$strpos=rand(1,6);imagestring($im,5,$strx,$strpos, substr($num,$i,1), $black); $strx+=rand(8,12);}ImagePNG($im);ImageDestroy($im);?>使用方法:本程序可以直接运行,运行之后即可看到一个图形验证码,每次刷新都随机生成新码。

php实现验证码制作

php实现验证码制作

php实现验证码制作php实现验证码制作验证码分为:数字验证码,字母验证码,数字加字母验证码,图片验证码,汉子验证码,视频验证码等!由于原理相同,且根据平时的使用范围来看,今天在这里只讲数字验证码,字母验证码,数字加字母验证码。

下面是由店铺为大家整理的php实现验证码制作,喜欢的可以收藏一下!了解更多详情资讯,请关注店铺!首先,看一张图了解验证码生成的过程。

(1)生成验证码底图(2)验证码内容(3)生成验证码(4)对比校验验证码实现的.核心技术分析(a)底图的实现,并添加干扰元素(b)生成验证内容(c)验证内容保存在服务端(d)验证内容的校验下面看代码实现的过程<?php/*这段代码实现了产生随机数字,随机数字+字母验证码*/session_start();$image = imagecreatetruecolor(100, 30);//imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。

$bgcolor = imagecolorallocate($image, 255, 255, 255);// imagecolorallocate —为一幅图像分配颜色imagefill($image,0,0,$bgcolor);/* 生成字母验证码for($i=0;$i<4;$i++){$fontsize = 6;// $fontcolor = imagecolorallocate($image, 0, 0, 0);$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));$fontcontent =rand(0,9);$x = ($i*100/4) + rand(5,10);$y = rand(5,10);// imagestring —水平地画一行字符串imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);}*/// 生成字母加数字的随机验证码$captcha_code = "";for($i=0;$i<4;++$i){$fontsize = 6;$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));$data = "abcdefghijklmnopqrstuvwxtz123456789";$fontcontent = substr($data,rand(0,strlen($data)),1);$captcha_code.=$fontcontent;$x = ($i*100/4) + rand(5,10);$y = rand(5,10);imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);}$_SESSION['authcode']=$captcha_code;// 给验证码添加点干扰项for($i=0;$i<200;$i++){$pointcolor = imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200));imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);// bool imagesetpixel ( resource $image , int $x , int $y , int $color );// imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。

php最简单的验证码

php最简单的验证码

php最简单的验证码create_code.php<?phpsession_start();//生成验证码图片header("Content-type: image/png");// 全数字$str = "1,2,3,4,5,6,7,8,9,a,b,c,d,f,g"; //要显示的字符,可自己进行增删$list = explode(",", $str);$cmax = count($list) - 1;$verifyCode = '';for ( $i=0; $i < 5; $i++ ){$randnum = mt_rand(0, $cmax);$verifyCode .= $list[$randnum]; //取出字符,组合成为我们要的验证码字符}$_SESSION['code'] = $verifyCode; //将字符放入SESSION 中$im = imagecreate(58,28); //生成图片$black = imagecolorallocate($im, 0,0,0); //此条及以下三条为设置的颜色$white = imagecolorallocate($im, 255,255,255);$gray = imagecolorallocate($im, 200,200,200);$red = imagecolorallocate($im, 255, 0, 0);imagefill($im,0,0,$white); //给图片填充颜色//将验证码绘入图片imagestring($im, 5, 10, 8, $verifyCode, $black); //将验证码写入到图片中for($i=0;$i<50;$i++) //加入干扰象素{imagesetpixel($im, rand()p , rand()0 , $black); //加入点状干扰素imagesetpixel($im, rand()p , rand()0 , $red);imagesetpixel($im, rand()p , rand()0 , $gray);//imagearc($im, rand()p, rand()p, 20, 20, 75, 170, $black); //加入弧线状干扰素//imageline($im, rand()p, rand()p, rand()p, rand()p, $red); //加入线条状干扰素}imagepng($im);imagedestroy($im);>引用demo.html<!-- DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "/DTD/xhtml-mobile10.dtd" --><html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title></title></head><body><form action="act.php" method="post"><input type="text" name="code" /><img id="code" src="create_code.php" alt="看不清楚,换一张" style="cursor: pointer; vertical-align:middle;" onClick="create_code()"/><!--<button type="button" onClick="create_code()">更换</button>--><button type="submit">提交</button></form><script>function create_code(){document.getElementByIdx_x('code').src = 'create_code.php?'+Math.random()*10000;}</script></body></html>//处理,判断是否输入正确act.php<?phpsession_start();if($_POST['code'] == $_SESSION['code']){echo 'ok';}else{echo 'no';}>。

PHP教程(22)项目使用+SQL注入+验证码制作+封装验证码码类

PHP教程(22)项目使用+SQL注入+验证码制作+封装验证码码类

PHP教程(22)项目使用+SQL注入+验证码制作+封装验证码码类回顾会话技术:cookie和sessioncookie:将数据保存到浏览器(HTTP协议设置响应头)设置cookie:setcookie(名字,值,过期时间,作用域,域名);读取cookie:$_COOKIEsession:将数据保存到服务器(默认是文件形式存在),依赖cookie(PHPSESSID=sessionid)流程:开启session(session_start);$_SESSION处理数据(读和写);写入session/session_destroy什么时候用cookie?什么时候用session?数据安全性要求较高的使用session;安全性不高的就存放到cookie。

面试题:浏览器可以禁用cookie,session基于cookie实现。

如果浏览器禁用了cookie,还能使用session吗?如果可以,如何实现?session是基于cookie,需要cookie保存sessionid,如果禁用了cookie,session不能脱离cookie。

如果用户在浏览器端自己禁用了cookie,用户得自己承担不能使用网站的责任。

可以模拟:利用a标签的href,但是可以通过修改配置项,让系统自动的为a 标签增加sessionID参数,并且session_start能自动去获取这些信息修改配置文件禁用只允许使用cookie保存sessionid 的配置使用a标签的href属性来保存sessionid效果1:系统自动为a标签增加sessionid效果2:系统自动在session_start的时候从a标签中的url中获取sessionid利用session_id和session_name函数,session_name获取PHPSESSID,session_id可以获取sessionid的值,还可以修改sessionid的值记住用户登录信息1. 在登录界面的时候,给用户提供一个表单选项,让用户能够选择是否记住用户信息。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

PHP验证码如何生成类完整代码
清源分享给大家的这款php验证码生成类灵活好用,用户可以定义各个成员有宽、高、画布、字数、类型、画类型同时我们只要修改 $Type就可以定义生成的是纯数字 , 纯小写字母,大小写数字混合,有需要的朋友可以借鉴参考。

代码:
<?php
class Code{
// 1. 定义各个成员有宽、高、画布、字数、类型、画类型
private $width; //宽度
private $height; //高度
private $num; //验证码字数
private $imgType; //生成图片类型
private $Type; //字串类型 1,2,3 三个选项 1 纯数字 2 纯小写字母 3 大小写数字混合
private $hb; //画布
public $codestr; // 验证码字串
public function __construct($height=20,$num=4,$imgType="jpeg",$Type=1){
$this->width = $num*20;
$this->height = $height;
$this->num = $num;
$this->imgType = $imgType;
$this->Type = $Type;
$this->codestr = $this->codestr();
$this->zuhe();
}
// 2. 定义随机获取字符串函数
private function codestr(){
switch($this->Type){
case 1: // 类型为1 获取1-9随机数
$str = implode("",array_rand(range(0,9),$this->num));
break;
case 2: // 类型为2 获取a-z随机小写字母
$str = implode("",array_rand(array_flip(range(a,z)),$this->num));
break;(PS:T不错的PHP Q扣峮:276167802,验证:wk)
case 3: // 类型为3 获取数字,小写字母,大写字母混合
for($i=0;$i<$this->num;$i++){
$m = rand(0,2);
switch($m){
case 0:
$o = rand(48,57);
break;
case 1:
$o = rand(65,90);
break;
case 2:
$o = rand(97,122);
break;
}
$str .= sprintf("%c",$o);
}
break;
}
return $str;
}
// 3. 初始化画布图像资源
private function Hb(){
$this->hb = imagecreatetruecolor($this->width,$this->height);
}
// 4. 生成背景颜色
private function Bg(){
return imagecolorallocate($this->hb,rand(130,250),rand(130,250),rand(130,250));
}
// 5. 生成字体颜色
private function Font(){
return imagecolorallocate($this->hb,rand(0,100),rand(0,100),rand(0,100));
}
// 6. 填充背景颜色
private function BgColor(){
imagefilledrectangle($this->hb,0,0,$this->width,$this->height,$this->Bg());
}
// 7. 干扰点
private function ganrao(){
$sum=floor(($this->width)*($this->height)/3);
for($i=0;$i<$sum;$i++){
imagesetpixel($this->hb,rand(0,$this->width),rand(0,$this->height),$this->Bg());
}
}
// 8. 随机直线弧线
private function huxian(){
for($i=0;$i<$this->num;$i++){
imageArc($this->hb,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand( 0,$this->height),rand(0,360),rand(0,360),$this->Bg());
}
}
// 9. 写字
private function xiezi(){
for($i=0;$i<$this->num;$i++){
$x=ceil($this->width/$this->num)*$i;
$y=rand(1,$this->height-15);
imagechar($this->hb,5,$x+4,$y,$this->codestr[$i],$this->Font());
}
}
// 10. 输出
private function OutImg(){
$shuchu="image".$this->imgType;
$header="Content-type:image/".$this->imgType;
if(function_exists($shuchu)){
header($header);
$shuchu($this->hb);
}else{
exit("GD库没有此类图像");
}
}
// 11. 拼装
private function zuhe(){
$this->Hb();
$this->BgColor();
$this->ganrao();
$this->huxian();
$this->xiezi();
$this->OutImg();
}
public function getCodeStr(){
return $this->codestr;
}
}
?>
来源:清源教育。

相关文档
最新文档