zoukankan      html  css  js  c++  java
  • PHP验证码类

    <?php 
    
    class verify{
    
    	//随机因子
    	private $text = 'abcdefghWddsdffgdWRFJKFJDFjjdfglkdfg883498Jhdsd9345345j340fg343o4j39d9t345j9dtej';
    
    	//验证码句柄资源
    	private $code;
    
    	//验证码长度
    	private $length = 4;
    
    	//验证码字体大小
    	private $size = 20;
    
    	//生成图像的高度
    	private $height = 30;
    
    	//验证码字体
    	private $font;
    
    	//图像资源句柄
    	private $image;
    
    	/**
    	 *	构造方法初始化
    	 */
    	public function __construct(){
    		$this->font = './static/fonts/msyh.ttf';
    	}
    
    	/**
    	 *	生成随机码
    	 */
    	private function createcode(){
    		$textlength = strlen($this->text)-1;
    		for($i=0; $i<$this->length; $i++){
    			$this->code .= $this->text[rand(0,$textlength)];
    		}
    	}
    
    	/**
    	 *	创建图像
    	 */
    	private function createimg(){
    		//创建画布
    		$this->image = imagecreatetruecolor($this->size*$this->length, $this->height);
    
    		//随机背景色
    		$color = imagecolorallocate($this->image, rand(0,255), rand(100,255), 175);
    
    		//创建矩形
    		imagefilledrectangle($this->image, 0, $this->height, $this->size*$this->length, 0, $color);
    	}
    
    	/**
    	 *	生成文字
    	 */
    
    	private function createtext(){
    		for($i=0;$i<$this->length;$i++){
    			$color = imagecolorallocate($this->image, 255, 255, 255);
    			imagettftext($this->image, $this->size, 0, 2, 25, $color, $this->font, $this->code);
    		}
    
    		//设置字符间距
    		//imagepsbbox($this->text, $this->font, $this->size);
    	}
    
    	/**
    	 *	生成干扰线 以及 $string字符
    	 */
    	private function createline(){
    		$string = '--';
    		for($i=0; $i<1; $i++){
    			$color = imagecolorallocate($this->image, rand(200,255), rand(200,255), rand(200,255));
    			imageline($this->image, rand(1,$this->size*$this->length), rand(1,$this->height), rand(1,$this->size*$this->length), rand(1,$this->height), $color);
    		}
    		for($i=0; $i<10; $i++){
    			$color = imagecolorallocate($this->image, rand(0,10), rand(50,150), rand(200,255));
    			imagestring($this->image, 1, rand(0,$this->size*$this->length), rand(0,$this->height), $string, $color);
    		}
    	}
    
    	/**
    	 *	输出图像
    	 */
    	private function outimage(){
    		header('content-type:image/png');
    		imagepng($this->image);
    		imagedestroy($this->image);
    	}
    
    	/**
    	 *	对外生成验证
    	 */
    	public function doimage(){
    		$this->createimg();
    		$this->createcode();
    		$this->createtext();
    		$this->createline();
    		$this->outimage();
    	}
    
    	//获取验证码  
    	public function getverify() {  
        	return strtolower($this->code);
        }
    
    }
    
    ?>
    

      

  • 相关阅读:
    [Redis知识体系] 一文全面总结Redis知识体系
    MySQL数据导入到ClickHouse
    docker本地搭建clickhouse
    【linux】修改宝塔默认的PHP CLI版本
    windows 10 安装go环境
    docker安装centos8
    Bootstrap 简洁、直观、强悍的前端开发框架,让web开发更迅速、简单。
    C#调用WebService
    登录时,记住用户的帐号密码
    asp.net,cookie,写cookie,取cookie
  • 原文地址:https://www.cnblogs.com/chenshuo/p/3672101.html
Copyright © 2011-2022 走看看