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

php中文网
发布: 2016-06-13 11:21:36
原创
784人浏览过

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

<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-&gt;img_width = $img_width;<br>		$this-&gt;img_height = $img_height;<br>		$this-&gt;font_size = $font_size;<br>		$this-&gt;line_count = $line_count;<br>	}<br><br>	//验证码生成主方法<br>	function createCodeImg(){<br>		$this-&gt;createGraphics();  <br>		$this-&gt;getChars();<br>		$this-&gt;setNoiceLine();<br><br>		$x = rand(2,5);<br>		$arr_X_Y = array(array($x,rand(1,3)),array($x+$this-&gt;font_size,rand(1,4)),array($x+2*$this-&gt;font_size,rand(1,3)),array($x+3*$this-&gt;font_size,rand(1,4)));<br>		print_r($arr_X_Y);<br>		for($i = 0;$i			$text_color = imagecolorallocate($this-&gt;img, rand(180,250), rand(180,250), rand(180,250));<br>			imagechar($this-&gt;img,$this-&gt;font_size,$arr_X_Y[$i][0],$arr_X_Y[$i][1],$this-&gt;arr_char[$i],$text_color); <br>		}<br>	}<br><br>     //创建画图板	<br>	function createGraphics(){<br>		header("Content-type: image/png"); <br>		$this-&gt;img = @imagecreatetruecolor($this-&gt;img_width, $this-&gt;img_height) or die("建立图像失败"); //创建图片<br>		$background_color = imagecolorallocate($this-&gt;img, 250, 250, 250);<br>		imagefill($this-&gt;img,0,0,$background_color);<br>		$border_color = imagecolorallocate($this-&gt;img,0,0,0); //边框色<br>		imagerectangle($this-&gt;img,0,0,$this-&gt;img_width,$this-&gt;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-&gt;img_height);<br>			$x2 = rand($this-&gt;img_width-20,$this-&gt;img_width-2);//结束位置<br>			$y2 = rand(2,$this-&gt;img_height);<br>			$line_color = imagecolorallocate($this-&gt;img, rand(180,250), rand(180,250), rand(180,250));<br>			imageline($this-&gt;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-&gt;arr_char[$i] = $this-&gt;str_chars[rand(0,56)];<br>				$strCode .= $this-&gt;arr_char[$i];<br>			}<br>			$this-&gt;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-&gt;arr_char = array($first_num,$operater,$second_num,"=");<div class="clear"></div>
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号