验证码

验证码样式

<?php
//namespace appservices;
/*
 * ValidateCode.php
 */
class ValidateCode {
    private $charset = '0123456789';
    private $code;
    private $codelen = 4;
    private $width = 163;
    private $height = 30;
    private $img;
    private $font;
    private $fontsize = 20;
    private $fontcolor;

    public function __construct($size) {
        $this->font = dirname(__file__) . '/t1.ttf';
        $this->codelen = $size;
        $this->charset = str_repeat($this->charset, 4);
    }

    private function createCode() {
        $_len = strlen($this->charset)-1;
        for ($i=0;$i<$this->codelen;$i++) {
            $this->code .= $this->charset[mt_rand(0,$_len)];
        }
    }

    private function createBg() {
        $this->img = imagecreatetruecolor($this->width , $this->height);
        $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);
    }

    private function createFont() {
        $_x = ($this->width - 10) / $this->codelen;
        for ($i=0;$i<$this->codelen;$i++) {
            $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
            imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);
        }
    }

    private function createLine() {
        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);
        }
        for ($i=0;$i<100;$i++) {
            $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
            imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
        }
    }

    private function outPut() {
        header('Content-type:image/png');
        imagepng($this->img);
        imagedestroy($this->img);
    }

    public function doimg() {
        $this->createBg();
        $this->createCode();
        $this->createLine();
        $this->createFont();
        $this->outPut();
    }

    public function getCode() {
        return strtolower($this->code);
    }
}
?>

调用类 

 上面的构造函数传入了值  此时我们实例化的时候也要传值进去

<?php
   include("ValidateCode.php");  //引入文件
  $code = new ValidateCode(4);  // 实例化类得到一个对象
   $code ->  doimg() ;   //调用 这个对象的 方法



?>

html 页面调用

<html>
    <body>
        
    <img src="text.php"  onclick="this.src='text.php?id='+Math.random()">
    <!-- 调用 text.php 文件  -->
    </body>



</html>
原文地址:https://www.cnblogs.com/Aleen/p/11233062.html