



下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 生物化学(第4版)课件 第7章 脂质代谢
- 超高性能混凝土框架节点抗震性能研究
- 儿童健康检查培训
- 卢桑斯的权变管理理论
- 黄疸的问诊要点与护理诊断
- 颈部疾病病人护理课件
- 我的健康早餐分享
- 幼儿春游欣赏活动方案设计
- 健康素养大讲堂
- 预防网络诈骗班会课件
- 美国技术服务合同英文翻译
- 企业数字化生存指南
- 医院医疗器械临床试验项目资料备案清单
- YDT 5206-2023宽带光纤接入工程技术规范
- 新疆警察学院面试问题及答案
- 小学三到六年级全册单词默写(素材)-2023-2024学年译林版(三起)小学英语
- 铁岭市高校毕业生“三支一扶”计划招募笔试真题2022
- 天然气泄漏事故演练方案及评估
- 《养老机构认知障碍照护专区设置与服务规范》
- 妇科炎症健康教育课件
- 儿科护理学(高职)全套教学课件
评论
0/150
提交评论