aklee 发表于 2009-11-11 10:17:58

发一个中文验证码的类[新鲜出炉]

昨天一个后台项目上需要用到,所以写了一个,欢迎拍砖


<?php

    /* PHPer:Aklee E-mail:Imaklee@gmail.com */

    session_start();

    {
    $ChineseChar = new ChineseCode();
    $ChineseChar -> AppearImg();
    }

    class ChineseCode{
            public $Img;
            public $BgColor;
            public $ImgWidth;
            public $ImgHeight;
            public $ImgLineNum;
            public $ImgLineColor;
            public $RandomNum;
            public $RandomNumColor;
            public $UseFont;
            public $UseFontSize;
            public $FontTextColor;
            public $FontIncline;
            public $AxesX;
            public $AxesY;
            public $CnText;

            function ChineseCode(){
                    $this->BgColor ="#dddddd";//画布背景颜色
                    $this->ImgWidth =257;//画布宽度
                    $this->ImgHeight =40;//画布高度
                    $this->ImgLineNum =rand(3,8);//乱线数
                    $this->ImgLineColor = "#eeeeee";//乱线颜色
                    $this->RandomNum = rand(100,200);//噪点数
                    $this->RandomNumColor = "#ff6600";//噪点颜色
                    $this->UseFont = "simhei.ttf";//使用字体
                    $this->UseFontSize = rand(16,20);//字体大小
                    $this->FontTextColor ="#000000";//字体颜色
                    $this->FontIncline = rand(-3,6);//字体倾斜度
                    $this->AxesX = rand(10,120);//x轴基本点
                    $this->AxesY = 30;//y轴基本点
                    $this->CnText = array("云","照","溪","满","楼","暮","野","花","冬","缓","响","雪","佰","农","海","邦","兴","悯","世","秋","江","泽","鼓","窗","春","月","子","苏","茶","酒","赋","诗","吞","羊","公","邑","紫","柳","晋","唐","龟","渔","奉","周","吠","翁","归","焚","吝","疾","禄","舟","吟","游","竹","露","栖","闷","训","师","橘","怀","蝗","苑","莱","殿","茵","苔","宫","庭","酿","舞","草","郁","雁","莺","猿","衡","楚","仆");
            }

            function AppearImg(){
               
                    $this->Img = imagecreatetruecolor($this->ImgWidth,$this->ImgHeight);
                       
                    $ImgBgColor = $this->getcolor($this->BgColor);
                       
                    /* $ImgLine有随机颜色和自定义两个相同的变量,用一个则要注释掉另一个,这里的$ImgLine默认为自定义;$ImgLine下面的$ImgDotColor同理 */
                    $ImgLine = $this->getcolor($this->ImgLineColor);
                    //$ImgLine = imagecolorallocate($this->Img,rand(0,255),rand(0,255),rand(0,255));
                       
                    //$ImgDotColor =$this->getcolor($this->RandomNumColor);
                    $ImgDotColor = imagecolorallocate($this->Img,rand(0,255),rand(0,255),rand(0,255));
                       
                    $FontTextColor = $this->getcolor($this->FontTextColor);
                    imagefilledrectangle($this->Img, 0, 0, $this->ImgWidth, $this->ImgHeight, $ImgBgColor);

                    for($i=0;$i<$this->ImgLineNum;$i++){
                            imageline($this->Img,0,rand(0,$this->ImgHeight),$this->ImgWidth,rand(0,$this->ImgHeight),$ImgLine);
                    }

                    for($i=0;$i<$this->RandomNum;$i++){
                            imagesetpixel($this->Img,rand()%$this->ImgWidth,rand()%$this->ImgHeight,$ImgDotColor);
                    }

                    for($i=0;$i<4;$i++) $text .= $this->CnText[(array_rand($this->CnText))];
                    imagettftext($this->Img,$this->UseFontSize,$this->FontIncline,$this->AxesX,$this->AxesY,$FontTextColor,$this->UseFont,$text);

                    $_SESSION['cncode'] = md5($text);


                    header("Content-type: image/png");
                    imagepng($this->Img);
                    imagedestroy($this->Img);
            }

            function getcolor($color){
                    $color = eregi_replace ("^#","",$color);
                    $r = $color.$color;
                    $r = hexdec ($r);
                    $b = $color.$color;
                    $b = hexdec ($b);
                    $g = $color.$color;
                    $g = hexdec ($g);
                    $color = imagecolorallocate ($this->Img, $r, $b, $g);
                    return $color;
            }



    }

?>

xjw2611845 发表于 2009-12-20 17:54:43

不好运行:Q

yjj798 发表于 2010-4-13 12:50:09

php的怎么可能运行
页: [1]
查看完整版本: 发一个中文验证码的类[新鲜出炉]