[php tools]验证码类
来源:互联网 发布:信息化时代数据 编辑:程序博客网 时间:2024/06/15 21:27
验证码类,调试了好久,最开始一直都是图片输出不出来,出来就是破损的。
最后,好不容易解决了。
原因是,不能够有文字的输出,当时为了测试在生成了字符串之后 echo 了字符串
暂时不知道为什么不能这样混合输出,找到原因了再来。
送上验证码代码,以上,如下:
<?php class Vcode { const BGLOW = 225; // 背景颜色深度最低值 const BGHIGH = 255; // 背景颜色深度最高值 const FTBGLOW = 0; // 字体颜色深度最低值 const FTBGHIGH = 100; // 字体颜色深度最高值 const FTSZLOW = 8; // 字体大小最低值 const FTSZHIGH = 10; // 字体大小最高值 const YLOW = 0; // Y轴向下最低值 const YHIGH = 8; // Y轴向下最高值 const PIXEL = 100; // 干扰点数量 const LINE = 4; // 干扰线数量 const PXLLOW = 0; // 干扰点颜色深度最低值 const PXLHIGH = 255; // 干扰点颜色深度最高值 const LINELOW = 0; // 干扰线颜色深度最低值 const LINEHIGH = 255; // 干扰线颜色深度最高值 private $w; // 图片宽度 private $h; // 图片高度 private $img; // 图片资源 private $n; // 验证码个数 private $code; // 验证码字符串 /** 构造方法,设置验证码宽,高和字符数量 */ function __construct($width, $height, $number) { $this->w = $width; $this->h = $height; $this->n = $number; $this->code = $this->CreateCode(); } /** 析构方法,销毁图片资源 */ function __destruct() { if ($this->img) { imagedestroy($this->img); } } /** 获取验证码字符串的接口 */ function GetCode() { return $this->code; } /** 提供输出图片的接口 */ function PrintCodeImage() { // 创建图片(设置背景) $this->CreateBackground(); // 创建字符(字母,数字) $this->CreateChars(); // 创建干扰(线,点) $this->CreateDisturb(); // 打印图片 $this->PrintImg(); } /*---------------------------以下为封装方法----------------------------*/ /** 生成验证码字符串 */ private function CreateCode() { $codes = "3456789"; $codes .= "abcdefghijkmnpqrstuvwxy"; $codes .= "ABCDEFGHJKLMNPQRSTUVWXY"; $code = ""; for ($i = 0; $i != $this->n; $i++) { $sit = rand(0, strlen($codes) - 1); $code .= $codes[$sit]; } return $code; } /** 创建图片并设置背景 */ private function CreateBackground() { $this->img = imagecreatetruecolor($this->w, $this->h); $bgcolor = imagecolorallocate( $this->img, rand(self::BGLOW,self::BGHIGH), rand(self::BGLOW,self::BGHIGH), rand(self::BGLOW,self::BGHIGH)); imagefill($this->img, 0, 0, $bgcolor); } /** 创建字符 */ private function CreateChars() { // 字符间距 $gap = $this->w / ($this->n + 1); $x = 0; // 初始位置 for ($i = 0; $i != $this->n; $i++) { $fontsize = rand(self::FTSZLOW, self::FTSZHIGH); $x += $gap; $textcolor = imagecolorallocate( $this->img, rand(self::FTBGLOW, self::FTBGHIGH), rand(self::FTBGLOW, self::FTBGHIGH), rand(self::FTBGLOW, self::FTBGHIGH)); imagestring( $this->img, $fontsize, $x, rand(self::YLOW, self::YHIGH), $this->code[$i], $textcolor); } } /** 创建干扰 */ private function CreateDisturb() { // 创建干扰点 for ($i = 0; $i != self::PIXEL; $i++) { $pxlcolor = imagecolorallocate( $this->img, rand(self::PXLLOW, self::PXLHIGH), rand(self::PXLLOW, self::PXLHIGH), rand(self::PXLLOW, self::PXLHIGH)); imagesetpixel( $this->img, rand(0, $this->w), rand(0, $this->h), $pxlcolor); } // 创建干扰线 for ($i = 0; $i != self::LINE; $i++) { $linecolor = imagecolorallocate( $this->img, rand(self::LINELOW, self::LINEHIGH), rand(self::LINELOW, self::LINEHIGH), rand(self::LINELOW, self::LINEHIGH)); // 图片资源 圆心x 圆心y 椭圆宽 椭圆高 弧度起点 弧度终点 颜色 imagearc( $this->img, 0, $this->h / 2, rand(0, 2 * $this->w), rand($this->h, 2 * $this->w), 0, 360, $linecolor); } } /** 输出图片 */ private function PrintImg() { // function_exists用于判断该服务器是否含有该函数 if (function_exists("imagepng")) { header("Content-Type:image/png"); imagepng($this->img); } else if (function_exists("imagegif")) { header("Content-Type:image/gif"); imagegif($this->img); } else if (function_exists("imagejpeg")) { header("Content-Type:image/jpeg"); imagejpeg($this->img); } else { die("No image support in this PHP server !"); } } }?>
0 0
- [php tools]验证码类
- php验证码类
- php验证码类
- PHP验证码类
- php验证码类
- php 验证码类
- php 验证码类
- PHP 验证码类
- PHP验证码类
- php验证码类
- php验证码类
- php验证码类
- php验证码类
- php验证码类
- php验证码类
- php验证码类
- php验证码类
- PHP验证码类,简单安全的PHP验证码
- 在类中增加一个对数据成员赋初值的成员函数(第二章第四题)
- UESTC 149 -- 解救小Q (bfs)
- java用户登录的小小程序
- mysql高可用性设置 --- mysql-mmm注意点
- 事务的commit work与rollback work
- [php tools]验证码类
- 蓝桥杯 猜算式
- IO流第十五课,总结
- Rotate Image --LeetCode
- 数据结构学习笔录--堆栈的实现
- BZOJ-2038-小Z的袜子hose-莫队
- 什么是nib文件
- Leetcode: Sum Root to Leaf Numbers
- SELECT INTO 和 INSERT INTO SELECT 两种表复制语句