当前位置 : 主页 > 网络编程 > PHP >

code.class.php

来源:互联网 收集:自由互联 发布时间:2021-06-28
code.class.php image = imagecreatetruecolor($this-width,$this-height); $bgColor = imagecolorallocate($this-image,255,255,255); imagefill($this-image,0,0,$bgColor); } /** * 生成验证码字符 */ private function createCode() { for ($i=0;$
code.class.php
 image = imagecreatetruecolor($this->width,$this->height);
        $bgColor = imagecolorallocate($this->image,255,255,255);
        imagefill($this->image,0,0,$bgColor);
    }

    /**
     * 生成验证码字符
     */
    private function createCode()
    {
        for ($i=0;$i<$this->codeLeng;$i++)
        {
            $fontContent = $this->codeString[rand(0, 35)];
            $this->code .= $fontContent;

            $fontColor = imagecolorallocate($this->image, rand(0, 120), rand(0, 120), rand(0, 120));
            $x = ($i * $this->width / $this->codeLeng) + rand(10, 10);
            $y = rand(10, 10);
            imagestring($this->image, $this->fontSize, $x, $y, $fontContent, $fontColor);
        }
        $_SESSION['code'] = $this->code;
    }

    /**
     * 添加圆点干扰项
     */
    private function setPixel()
    {
        for ($i=0;$i<200;$i++)
        {
            $pixelColor = imagecolorallocate($this->image,rand(100,200),rand(100,200),rand(100,200));
            imagesetpixel($this->image,rand(0,$this->width-1),rand(1,$this->height-1),$pixelColor);
        }
    }

    /**
     * 线性干扰
     */
    private function setLine()
    {
        for ($i=0;$i<20;$i++)
        {
            $linelColor = imagecolorallocate($this->image,rand(100,200),rand(100,200),rand(100,200));
            imageline($this->image,rand(0,$this->width-1),rand(1,$this->height-1),rand(0,$this->width-1),rand(1,$this->height-1),$linelColor);
        }
    }


    /**
     * 输出验证码
     *
     */
    private function outPut()
    {
        header('content-type: image/png');
        imagepng($this->image);
        $this->destroyImage();
    }

    /**
     * 显示验证码
     */
    public function showCode()
    {
        $this->createImage();
        $this->setPixel();
        $this->setLine();
        $this->createCode();
        $this->outPut();
    }

    /**
     * 销毁图片资源
     */
    private function destroyImage()
    {
        imagedestroy($this->image);
    }

}
上一篇:php 短网址.txt
下一篇:PHP端支持跨域.php
网友评论