Home > Backend Development > PHP Tutorial > 验证码的一个类,如何就是不出来图片呢

验证码的一个类,如何就是不出来图片呢

WBOY
Release: 2016-06-13 12:48:18
Original
860 people have browsed it

验证码的一个类,怎么就是不出来图片呢
自己闲着没事,自定义了一个验证码生成的类,但是不知道为什么,图片就是现实不出来。还请高手指点一下。

<br>
<?php  <br />
class imageCode{<br>
	public $img_width;    //验证码宽度<br>
	public $img_height;		//高度<br>
	public $arr_char = array();  //验证码上显示的字符<br>
	public $font_size;         //字体大小<br>
	public $img;				//验证码图片<br>
	public $code_result;		//要保存在session中的数或者字符串<br>
	public $line_count = 7;		//噪音线数量<br>
	private $str_chars = "0123456789abcdefghjkmnpqrstuvwxyzABCEDFGHJKLMNPQRSTUVWXYZ";//验证码字符串<br>
	<br>
	//构造函数<br>
	function __construct($img_width,$img_height,$font_size,$line_count){    <br>
		$this->img_width = $img_width;<br>
		$this->img_height = $img_height;<br>
		$this->font_size = $font_size;<br>
		$this->line_count = $line_count;<br>
	}<br>
	<br>
	//验证码生成主方法<br>
	function createCodeImg(){<br>
		$this->createGraphics();  <br>
		$this->getChars();<br>
		$this->setNoiceLine();<br>
		<br>
		$x = rand(2,5);<br>
		$arr_X_Y = array(array($x,rand(1,3)),array($x+$this->font_size,rand(1,4)),array($x+2*$this->font_size,rand(1,3)),array($x+3*$this->font_size,rand(1,4)));<br>
		print_r($arr_X_Y);<br>
		for($i = 0;$i
			$text_color = imagecolorallocate($this->img, rand(180,250), rand(180,250), rand(180,250));<br>
			imagechar($this->img,$this->font_size,$arr_X_Y[$i][0],$arr_X_Y[$i][1],$this->arr_char[$i],$text_color); <br>
		}<br>
	}<br>
<br>
     //创建画图板	<br>
	function createGraphics(){<br>
		header("Content-type: image/png"); <br>
		$this->img = @imagecreatetruecolor($this->img_width, $this->img_height) or die("建立图像失败"); //创建图片<br>
		$background_color = imagecolorallocate($this->img, 250, 250, 250);<br>
		imagefill($this->img,0,0,$background_color);<br>
		$border_color = imagecolorallocate($this->img,0,0,0); //边框色<br>
		imagerectangle($this->img,0,0,$this->img_width,$this->img_height,$border_color);<br>
	}<br>
	<br>
     //画噪音线<br>
	function setNoiceLine(){<br>
		for($i = 0;$i line_count;$i ++){<br>
			$x1 = rand(3,20);//开始位置<br>
			$y1 = rand(2,$this->img_height);<br>
			$x2 = rand($this->img_width-20,$this->img_width-2);//结束位置<br>
			$y2 = rand(2,$this->img_height);<br>
			$line_color = imagecolorallocate($this->img, rand(180,250), rand(180,250), rand(180,250));<br>
			imageline($this->img,$x1,$y1,$x2,$y2,$line_color); <br>
		}<br>
	}<br>
<br>
	//产生随机字符串或者加减字符	<br>
	function getChars(){<br>
		$strCode = "";<br>
		if(rand(0,1) == 1){//字符串类型的 验证码<br>
			for($i = 0;$i 
				$this->arr_char[$i] = $this->str_chars[rand(0,56)];<br>
				$strCode .= $this->arr_char[$i];<br>
			}<br>
			$this->code_result = $strCode;<br>
		}<br>
		else{			//加减类型验证码<br>
			$first_num = rand(1,10);<br>
			$second_num = 0;<br>
			$result = 0;<br>
			$arr_operater = array("+","-");<br>
			$operater = $arr_operater[rand(0,1)];<br>
			switch ($operater){<br>
				case "+":<br>
					$second_num = rand(0,10);<br>
					$result = $first_num + $second_num;<br>
					break;<br>
				case "-":<br>
					$second_num = rand(0,$first_num);<br>
					$result = $first_num - $second_num;<br>
					break;<br>
			}<br>
			$this->arr_char = array($first_num,$operater,$second_num,"="); <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template