<?php<br />/**---------------------------------------------------------------------------*/<br />/** PHP 數字運算 驗證碼類<br />/** @著作權注釋<br />/** 原創 張燦庭 如果您有任何疑問和想法可以發郵件(123294587@qq.com)<br />/** 本類允許轉載、複製和修改,但轉載、複製和修改的同時請保留原始的出處和作者聲明,這也是對作者勞動成果的一種尊重!<br />/**---------------------------------------------------------------------------*/</p><p>// 開啟session<br />session_start(); </p><p>//頭部 防止緩衝<br />header("Cache-Control: no-cache, must-revalidate");<br />header("Pragma: no-cache");<br />header('Content-Type:image/gif');</p><p>//調用類<br />$code = new codeClass();<br />$code->transparent= true;<br />$code->solid= true;<br />$code->pixel= 20;<br />$code->line= 0;<br />$code->arc = 0;<br />$code->dashed= 0;<br />$code->output('authcode');<br />unset($code);</p><p>class codeClass {<br />public $width = 80;//高度<br />public $height= 20;//寬度<br />public $transparent= true;//透明<br />public $solid= true;//邊框<br />public $dashed= 0;//虛線<br />public $arc= 0;//弧線<br />public $line= 0;//直線<br />public $pixel= 0;//幹擾點<br />private $img;//對象</p><p>//輸出驗證碼<br />public function output($name){</p><p>$this->create();//建立對象<br />$this->drawBg();//白色背景<br />$this->drawAngle();//邊框<br />$this->drawPixel();//幹擾像素<br />$this->drawLine();//幹擾線<br />$this->drawArc();//弧線<br />$this->drawDashed(); //虛線<br />$this->drawText($name); //寫入文字<br />//銷毀對象<br />imagedestroy($this->img);<br />}</p><p>//建立對象<br />private function create(){<br />$this->width = empty($this->width) ? 80 : $this->width;<br />$this->height = empty($this->height) ? 20 : $this->height;<br />$this->img = imagecreatetruecolor($this->width, $this->height);<br />imagecolorallocate($this->img, 0, 0, 0);<br />}</p><p>//背景<br />private function drawBg(){<br />if($this->transparent == true){<br />imagefill($this->img, 0, 0, imagecolorallocate($this->img, 255, 255, 255));//填充白色<br />//imagecolortransparent($this->img, imagecolorallocate($this->img, 255, 255, 255));//背景透明<br />}<br />}</p><p>//邊框<br />private function drawAngle(){<br />//灰色邊框<br />if($this->solid == true){<br />imagerectangle($this->img, 0, 0, $this->width-1, $this->height-1, imagecolorallocate($this->img, 204, 204, 204));<br />}<br />}</p><p>//輸出驗證碼<br />public function drawText($name){<br />//數組<br />$math= $this->getText();<br />$x = 5;//出生點<br />//迴圈數組<br />foreach($math[0] as $value){<br />//文字顏色<br />$rgb= $this->getColor();<br />$color = imagecolorallocate($this->img,$rgb['r'], $rgb['g'], $rgb['b']);<br />//位置<br />$y= ($this->height - imagefontwidth(5)) / 2; // Y軸 置中<br />imagestring($this->img, 5, $x, $y, $value, $color);<br />$x = $x + imagefontwidth(5) * strlen($value);// X軸<br />}<br />//儲存 session<br />$_SESSION[$name] = $answer;<br />//輸出映像<br />imagegif($this->img);<br />}</p><p>//等式 答案<br />private function getText(){<br />//數值數組<br />$math = array(<br />1,<br />1,<br />1,<br />1,<br />5,<br />6,<br />7,<br />8,<br />9,<br />10,<br />);<br /> //隨機<br />$x = $math[array_rand($math, 1)];<br />$y = $math[array_rand($math, 1)];<br />$array = array();<br />$array[]= array($x, '+', $y, ' =', '?'); //算術運算式<br />$array[]= $x + $y; //答案<br />return $array;<br />}</p><p>//文字顏色<br />private function getColor(){<br />$color = array();<br />//暗色<br />$color[]= '#000080';<br />$color[]='#0000EE';<br />$color[]='#008B00';<br />$color[]='#009ACD';<br />$color[]='#191970';<br />$color[]='#1C86EE';<br />$color[]='#2F4F4F';<br />$color[]='#4B0082';<br />//亮色<br />$color[]='#CD0000';<br />$color[]='#8E8E38';<br />$color[]='#A0522D';<br />$color[]='#EE1289';<br />$color[]='#FF0000';<br />$color[]='#FF4500';<br />$rgb= $color[array_rand($color, 1)];<br />return $this->hColor2RGB($rgb);<br />}</p><p>//轉換為rgb<br />function hColor2RGB($hexColor) {<br /> $color = str_replace('#', '', $hexColor);<br /> $rgb= array(<br /> 'r' => hexdec(substr($color, 0, 2)),<br /> 'g' => hexdec(substr($color, 2, 2)),<br /> 'b' => hexdec(substr($color, 4, 2))<br /> );<br />return $rgb;<br />}</p><p>//繪製橢圓<br />private function drawArc(){<br />if($this->arc > 1){<br />for ($i = 0; $i < $this->arc; $i++){<br />$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));<br />imagearc($this->img, rand(-$this->width, $this->width), rand(-$this->height, $this->height), $this->width, $this->height, $this->width, $this->height, $color);<br />}<br />}<br />}</p><p>//繪製虛線<br />private function drawDashed(){<br />if($this->dashed > 1){<br />for ($i = 0; $i < $this->dashed; $i++){<br />$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));<br />imagedashedline($this->img, rand(0, $this->width), 0, rand(0, $this->width), $this->height, $color);<br />}<br />}<br />}</p><p>//繪製對角線<br />private function drawLine (){<br />if($this->line > 1){<br />for ($i = 0; $i < $this->line; $i++){<br />$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));<br />imageline($this->img, rand(0, $this->width), 0, rand(0, $this->width), $this->height, $color);<br />}<br />}<br />}</p><p>//繪製噪點<br />private function drawPixel(){<br />if($this->pixel > 1){<br />for ($i = 0; $i < $this->pixel; $i++){<br />$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));<br />imagesetpixel($this->img, rand(0, $this->width), rand(0, $this->height), $color);<br />}<br />}<br />}<br />}<br />?>