圖片驗證碼文件,加減法計算方式


/**
 * 圖片驗證碼文件,加減法計算方式
 */
class ImageCode {
    private $jiashu = 0;//加數或者減數
    private $jianshu = 0;//被加數或者被減數
    private $yunsuan = '';//運算符
    private $deshu = 0;//得數
    private $string = '';//字符串樣式
    private $img;   //圖片對象
    private $width = 100;//圖片寬度
    private $height = 50;//圖片高度
    private $ttf = 'arial.ttf'; //字體文件
    private $session = 'code';//session 變量

    private function jiashu() {
        //header('Content-type:image/png');
        header("Content-type:image/JPEG");
        $this->jiashu = rand(1,10);
        $this->jianshu = rand(1,10);
        $this->yunsuan = $this->jiashu > $this->jianshu ? '-':'+';
        $this->deshu = $this->jiashu > $this->jianshu ? $this->jiashu - $this->jianshu:$this->jiashu + $this->jianshu;
    }
    public function show($w = 100,$h=50,$t='arial.ttf',$code='code') {
        $this->jiashu();
        $this->string = $this->jiashu.$this->yunsuan.$this->jianshu.'=?';
        $this->width =$w;
        $this->height = $h;
        $this->ttf = $t;
        $this->session = $code;
        session_start();
        $_SESSION[$this->session] = $this->deshu;
        $this->images();
    }

    private function images() {
        $this->img = imagecreate($this->width, $this->height);
        $background_color = imagecolorallocate($this->img, 255, 255, 255);
        imagecolortransparent($this->img,$background_color);
        imagettftext($this->img,14,0,1,20,imagecolorallocate($this->img, 0, 0, 0),$this->ttf,$this->string);
        $this->echoImages();
    }

    private function echoImages() {
        imagepng($this->img);//以PNG格式將圖像輸出到瀏覽器
        imagedestroy($this->img);//銷毀圖像
    }
}
$ImageCode = new ImageCode;
$ImageCode->show(130,35,'arial.ttf','code');


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM