下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、PHP验证码类实现验证码功能,有两个方法,分别是用内置字体和自定义字体生成验证码。具体代码如下:<?php/* 验证码生成类* example* $pic = new uImage();* $code = $pic->getVerifyCode(); * header(Content-type:image/png); * $pic->captchaFromFont($font=RAVIE.TTF); or $pic->captcha();*/class uImage /* 验证码字符* access protected*/protected $code;/* 生成图片验
2、证码,直接输出的是图片,字体大小是内置字体,最大是5* access public* param int $width 验证码图片宽度* param int $height 验证码图片的高度* param int $snow 背景雪花的数量* param int $line 干扰线的条数*/public function captcha($width=100, $height=30, $snow=80, $line=3) $pic = imagecreatetruecolor($width, $height);$backageColor = imagecolorallocate($pic, 0
3、xFF, 0xFF, 0xFF);imagefill($pic, 0, 0, $backageColor);/打雪花for($i=0; $i<=$snow;$i+) $color = imagecolorallocate($pic, mt_rand(150, 230), mt_rand(150, 230), mt_rand(150, 230);imagechar($pic, 1, mt_rand(0, $width), mt_rand(0, $height), *, $color);imagecolordeallocate ($pic, $color);/画干扰线for($i=0; $i
4、<=$line; $i+) $x1 = mt_rand(2, $width * 0.2);$x2 = mt_rand($width * 0.8, $width - 2);$y1 = mt_rand(2, $height - 2);$y2 = mt_rand(2, $height - 2);$color = imagecolorallocate($pic, mt_rand(130, 250), mt_rand(130, 250), mt_rand(130, 250);imageline($pic, $x1, $y1, $x2, $y2, $color);imagecolordealloca
5、te ($pic, $color);/画字符$code = $this->code;$eachW = $width / strlen($code); /图片依据字符个数分配等份数$fontWidth = imagefontwidth(5); /取得字体宽度$fontHeight = imagefontheight(5); /取得字体高度for($i=0; $i<strlen($code);$i+) $color = imagecolorallocate($pic, mt_rand(30, 155), mt_rand(30, 155), mt_rand(30, 150);$x = m
6、t_rand($eachW * $i, $eachW * ($i+1)-$fontWidth);$y = mt_rand(3, $height-$fontHeight);imagechar($pic, 5, $x, $y, $code$i, $color); /水平画字符imagecolordeallocate ($pic, $color);/输出ob_start();ob_clean();imagepng($pic);imagedestroy($pic);/* 根据自定义字体生成验证码* access public* param string $font 字符文件, TrueType 字体文
7、件,.ttf字体* param int $fontWeight 字符大小* param int $width 图片宽* param int $height 图片高* param int $snow 背景雪花个数* param int $line 干扰线条数* param int $padding 图片内边距*/public function captchaFromFont($font, $fontWeight=16, $width=100, $height=30, $snow=80, $line=3, $padding=3)if(!isset($font)return false;$pic =
8、 imagecreatetruecolor($width, $height);$backageColor = imagecolorallocate($pic, 0xFF, 0xFF, 0xFF);imagefill($pic, 0, 0, $backageColor);imagecolordeallocate ($pic, $backageColor);/打雪花for($i=0; $i<=$snow;$i+) $color = imagecolorallocate($pic, mt_rand(150, 230), mt_rand(150, 230), mt_rand(150, 230);
9、imagechar($pic, 1, mt_rand(0, $width), mt_rand(0, $height), *, $color);imagecolordeallocate ($pic, $color);/画干扰线for($i=0; $i<=$line; $i+) $x1 = mt_rand(2, $width * 0.2);$x2 = mt_rand($width * 0.8, $width - 2);$y1 = mt_rand(2, $height - 2);$y2 = mt_rand(2, $height - 2);$color = imagecolorallocate(
10、$pic, mt_rand(130, 250), mt_rand(130, 250), mt_rand(130, 250);imageline($pic, $x1, $y1, $x2, $y2, $color);imagecolordeallocate ($pic, $color);/画字符$code = $this->code;$eachW = $width / strlen($code); /图片依据字符个数分配等份数$codeArray = str_split($code);for($i=0; $i<count($codeArray); $i+)/取得字符宽高$fontbox
11、 = imagettfbbox($fontWeight, 0, $font, $codeArray$i);$fontWidth = $fontbox2 - $fontbox0;$fontHeight = $fontbox1 - $fontbox7;$color = imagecolorallocate($pic, mt_rand(30, 155), mt_rand(30, 155), mt_rand(30, 150); /字符颜色$angle = mt_rand(-20, 20); /字符角度if($i=0)$start = $eachW * $i+$padding;$end = $eachW
12、 * ($i+1)-$fontWidth;elseif($i = count($codeArray)$start = $eachW * $i;$end = $eachW * ($i+1)-$fontWidth-$padding;else$start = $eachW * $i;$end = $eachW * ($i+1)-$fontWidth-$padding;$x = $start < $end ? mt_rand($start, $end) : $start;$y = ($fontHeight+$padding) > $height ? $padding : mt_rand($
13、fontHeight+$padding, $height-$padding); imagettftext($pic, $fontWeight, $angle, $x, $y, $color, $font, $codeArray$i); /用 TrueType 字体向图像写入文本 imagecolordeallocate ($pic, $color);/输出ob_start();ob_clean();imagepng($pic);imagedestroy($pic);/* 获取验证码* access public* param int $len 验证码字符的长度* return strint 生成的验证码字符*/public function getVerifyCode($len=4)if(!isset($this->code)$this->code = $this->getCode($len);return $this->code;/* 生成验证码* access protected* param int $len 验证码字符的长度*
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025陕西西安市高陵区国企招聘2人笔试历年难易错考点试卷带答案解析
- 2025福建厦门市同安区第一建筑工程公司校园招聘79人笔试历年备考题库附带答案详解
- 2025浙江湖州南浔墨韵文化创意有限公司招聘2人笔试历年难易错考点试卷带答案解析
- 2025江苏灌江农业发展集团有限公司招聘10人笔试历年常考点试题专练附带答案详解
- 2025山西吕梁市交城县众恒信达人力资源服务有限公司招聘森林草原消防综合救援岗位拟聘用人员笔试历年备考题库附带答案详解
- 2025安徽宣城市兴港化工有限公司招聘6人笔试历年常考点试题专练附带答案详解
- 2025中化集团招聘人力资源管理实习生1人(北京)笔试历年典型考点题库附带答案详解
- 2026年综合管理与法律法规类考试大纲解析及模拟试题
- 2026年全员食品安全与健康饮食知识问答
- 2026年生物多样性知识要点详解
- 2024年山东地区光明电力服务公司招聘笔试真题
- 《脑出血护理查房范例》课件
- led显示屏安装协议合同书
- 中国地域性文化课件
- 2025至2030中国超导市场发展现状与前景展望预测研究报告版
- 植物化学保护试题及答案
- GB/T 3917.3-2025纺织品织物撕破性能第3部分:梯形试样撕破强力的测定
- (高清版)DB36∕T 1324-2020 公路建设项目档案管理规范
- 药物安全性监测-洞察分析
- 2025年厦门地铁运营公司招聘笔试参考题库含答案解析
- 2025年广西桂林市考试招聘部队随军家属33人高频重点提升(共500题)附带答案详解
评论
0/150
提交评论