加减乘除验证码

来源:互联网 发布:软件药品库 编辑:程序博客网 时间:2024/06/05 04:19
<?phpclass Captcha{   private $numX = 0;               //加数或者减数   private $numY = 0;               //被加数或者被减数   private $divX = 0;               //被除数   private $divY = 0;               //除数   private $operation = '';         //运算符   private $result = 0;             //得数   private $code = '';              //验证码字符串   private $img;                    //图片对象   private $width = 150;            //图片宽度   private $height = 40;            //图片高度   private $fontsize = 25;          //指定字体大小   private $font;                   //指定的字体   private $fontcolor;              //指定字体颜色   private $codeLen = 0;              //验证码长度   private $Session = 'trueCaptha';   //Session变量    //构造方法初始化    public function __construct($width=150,$height=40,$fontsize = 25,$font = null)    {        if ($font === null) {           /* $font = __DIR__ . "/elephant.ttf";*/           $font = './msyhbd.ttf';        }      $this->width = $width;      $this->height = $height;      $this->fontsize = $fontsize;      $this->font = $font;    } //生成数字并进行逻辑运算   private function createCaptcha()    {      header('Content-type:image/png');      $this->numX = rand(1, 10);      $this->numY = rand(1, 10);      //设计除数      $div = array(1,2,4,8);      shuffle($div);      shuffle($div);      shuffle($div);      $this->divX = max(array($div['0'], $div['1']));      $this->divY = min(array($div['0'], $div['1']));      $operators = array('+', '-', '*', '/');      shuffle($operators);      $this->operation = $operators['0'];    //获得一个运算符,并进行计算      switch($this->operation)      {         case '+':          $this->result = $this->numX + $this->numY;         break;         case '-':          $this->operation = $this->numX > $this->numY ? '-' : '+';          $this->result   = $this->numX > $this->numY ? $this->numX - $this->numY : $this->numX + $this->numY;         break;         case '*':          $this->result   = $this->numX * $this->numY;         break;         case '/':          $this->result   = $this->divX / $this->divY;         break;         default:          $this->result   = $this->numX + $this->numY;      }        //判断符号,处理除号       $operate = array('+','-','*');        if(in_array($this->operation,$operate)){          $this->code = $this->numX . $this->operation . $this->numY . ' = ? ';          //获取验证码长度          $this->codeLen = strlen($this->code);        }else{          $this->code = $this->divX . $this->operation . $this->divY . ' = ? ';          //获取验证码长度          $this->codeLen = strlen($this->code);        }   }   //制作验证码   public function Show( $W = 50, $H = 50, $Ttf = '\msyhbd.ttf', $key = 'trueCaptha' )   {        $this->createCaptcha();        $this->width = $W;        $this->height = $H;        $this->Ttf = $Ttf;        $this->Session= $key;        session_start();        $_SESSION[$this->Session] = $this->result;        $this->createBg();   }    //创建图片画布    private function createBg()    {        $this->img = imagecreatetruecolor($this->width, $this->height);        //获取图片宽高        $imgWidth = imagesx($this->img);        $imgHeight = imagesy($this->img);        //获取字体宽高        $fontWidth = imagefontwidth($this->fontsize);        $fontHeight = imagefontheight($this->fontsize);        //居中        $x = ($imgWidth-$fontWidth*$this->codeLen)/2;        $y = ($imgHeight - $fontHeight)/2;        //分配颜色        $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));        //矩形区域着色        imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);        //TTF文字到图中        imagettftext($this->img, 14, 0, $x, $this->height / 1.4, imagecolorallocate ($this->img, 0, 0, 0), $this->Ttf, $this->code );        //生成雪花        for ($i=0;$i<6;$i++) {            $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));            imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);        }        $this->EchoImages();    }   private function EchoImages(){    imagepng($this->img);    imagedestroy($this->img);   }}$Imagecode = new Captcha();$Imagecode -> Show(130, 35, 'msyhbd.ttf', 'code');
0 0
原创粉丝点击