<?php /**---------------------------------------------------------------------------*/ /** PHP 数字运算 验证码类 /** @版权注释 /** 原创 张灿庭 如果您有任何疑问和想法可以发邮件(123294587@qq.com) /** 本类允许转载、复制和修改,但转载、复制和修改的同时请保留原始的出处和作者声明,这也是对作者劳动成果的一种尊重! /**---------------------------------------------------------------------------*/ // 开启session session_start(); //头部 防止缓存 header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); header('Content-Type:image/gif'); //调用类 $code = new codeClass(); $code->transparent = true; $code->solid = true; $code->pixel = 20; $code->line = 0; $code->arc = 0; $code->dashed = 0; $code->output('authcode'); unset($code); class codeClass { public $width = 80; //高度 public $height = 20; //宽度 public $transparent = true; //透明 public $solid = true; //边框 public $dashed = 0; //虚线 public $arc = 0; //弧线 public $line = 0; //直线 public $pixel = 0; //干扰点 private $img; //对象 //输出验证码 public function output($name){ $this->create(); //建立对象 $this->drawBg(); //白色背景 $this->drawAngle(); //边框 $this->drawPixel(); //干扰像素 $this->drawLine(); //干扰线 $this->drawArc(); //弧线 $this->drawDashed(); //虚线 $this->drawText($name); //写入文字 //销毁对象 imagedestroy($this->img); } //建立对象 private function create(){ $this->width = empty($this->width) ? 80 : $this->width; $this->height = empty($this->height) ? 20 : $this->height; $this->img = imagecreatetruecolor($this->width, $this->height); imagecolorallocate($this->img, 0, 0, 0); } //背景 private function drawBg(){ if($this->transparent == true){ imagefill($this->img, 0, 0, imagecolorallocate($this->img, 255, 255, 255)); //填充白色 //imagecolortransparent($this->img, imagecolorallocate($this->img, 255, 255, 255)); //背景透明 } } //边框 private function drawAngle(){ //灰色边框 if($this->solid == true){ imagerectangle($this->img, 0, 0, $this->width-1, $this->height-1, imagecolorallocate($this->img, 204, 204, 204)); } } //输出验证码 public function drawText($name){ //数组 $math = $this->getText(); $x = 5; //出生点 //循环数组 foreach($math[0] as $value){ //文字颜色 $rgb = $this->getColor(); $color = imagecolorallocate($this->img, $rgb['r'], $rgb['g'], $rgb['b']); //位置 $y = ($this->height - imagefontwidth(5)) / 2; // Y轴 居中 imagestring($this->img, 5, $x, $y, $value, $color); $x = $x + imagefontwidth(5) * strlen($value); // X轴 } //保存 session $_SESSION[$name] = $answer; //输出图像 imagegif($this->img); } //等式 答案 private function getText(){ //数值数组 $math = array( 1, 1, 1, 1, 5, 6, 7, 8, 9, 10, ); //随机 $x = $math[array_rand($math, 1)]; $y = $math[array_rand($math, 1)]; $array = array(); $array[] = array($x, '+', $y, ' =', '?'); //算术表达式 $array[] = $x + $y; //答案 return $array; } //文字颜色 private function getColor(){ $color = array(); //暗色 $color[] = '#000080'; $color[] = '#0000EE'; $color[] = '#008B00'; $color[] = '#009ACD'; $color[] = '#191970'; $color[] = '#1C86EE'; $color[] = '#2F4F4F'; $color[] = '#4B0082'; //亮色 $color[] = '#CD0000'; $color[] = '#8E8E38'; $color[] = '#A0522D'; $color[] = '#EE1289'; $color[] = '#FF0000'; $color[] = '#FF4500'; $rgb = $color[array_rand($color, 1)]; return $this->hColor2RGB($rgb); } //转换为rgb function hColor2RGB($hexColor) { $color = str_replace('#', '', $hexColor); $rgb = array( 'r' => hexdec(substr($color, 0, 2)), 'g' => hexdec(substr($color, 2, 2)), 'b' => hexdec(substr($color, 4, 2)) ); return $rgb; } //绘制椭圆 private function drawArc(){ if($this->arc > 1){ for ($i = 0; $i < $this->arc; $i++){ $color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255)); imagearc($this->img, rand(-$this->width, $this->width), rand(-$this->height, $this->height), $this->width, $this->height, $this->width, $this->height, $color); } } } //绘制虚线 private function drawDashed(){ if($this->dashed > 1){ for ($i = 0; $i < $this->dashed; $i++){ $color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255)); imagedashedline($this->img, rand(0, $this->width), 0, rand(0, $this->width), $this->height, $color); } } } //绘制对角线 private function drawLine (){ if($this->line > 1){ for ($i = 0; $i < $this->line; $i++){ $color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255)); imageline($this->img, rand(0, $this->width), 0, rand(0, $this->width), $this->height, $color); } } } //绘制噪点 private function drawPixel(){ if($this->pixel > 1){ for ($i = 0; $i < $this->pixel; $i++){ $color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255)); imagesetpixel($this->img, rand(0, $this->width), rand(0, $this->height), $color); } } } } ?>