为了账号安全,请及时绑定邮箱和手机立即绑定

为啥我的报错呢?

http://img1.sycdn.imooc.com//59c5066b0001ccf509661046.jpg

<?php

 Class Captcha{

//字体文件

private $_fontfile ='';

//画布宽度和高度

private $_width =120;

private $_height =40;

    //字体大小

    private $_size =20;

    //验证吗长度

    private $_length =4;

    //画布资源

    private $_image =null;

    //干扰元素

    //雪花*个数

    private $_snow =0;

    //像素个数

    private $_pixel =0;

    //线段个数

    private $_line =0;

    /**

     * 初始化数据

     * @param  array  $config 

     */

public function _construct($config=array()){

if(is_array($config)&&count($config)>0){

//检测字体是否存在并且可读

if (isset($config['fontfile'])&&is_file('fontfile')&&is_readable('fontfile')) {

$this->_fontfile = $config['fontfile'];

}else{

return false;

}

//检测是否设置画布宽和高

if (isset($config['width'])&&$config['width']>0) {

$this->_width = (int)$config['width'];

}

if (isset($config['height'])&&$config['height']>0) {

$this->_height = (int)$config['height'];

}

if (isset($config['size'])&&$config['size']>0) {

$this->_size = (int)$config['size'];

   }

   //检测是否设置验证码长度

   if (isset($config['length'])&&$config['length']>0) {

$this->_length = (int)$config['length'];

   }

            $this->_image = imagecreatetruecolor($this->_width, $this->_height);

            return $this->_image;

   //配置干扰元素

   if (isset($config['snow'])&&$config['snow']>0) {

    $this->_snow = (int)$config['snow'];

   }

   if (isset($config['pixel'])&&$config['pixel']>0) {

    $this->_pixel = (int)$config['pixel'];

   }

   if (isset($config['line'])&&$config['line']>0) {

    $this->_line = (int)$config['line'];

   }

  

   }else{

    return false;

   }

    }

    /**

     * 得到验证码

     * @return [type] [description]

     */

    public function getCaptcha(){

    $white = imagecolorallocate($this->_image, 255,255,255);

    //填充矩形

    imagefilledrectangle($this->_image, 0, 0, $this->_width, $this->_height,$white);

    //生成验证码

    $str = $this->_generateStr($this->_length);

    if (false===$str) {

    return false;

    }

    //绘制验证码

    for ($i=0; $i < $this->_length; $i++) { 

    $size = $this->_size;

    $angle = mt_rand(-30,30);

    $x = ceil($this->_width/$this->_length)*$i+mt_rand(5,10);

    $y = ceil($this->_height/1.5);

            $color = $this->_getRandColor();

            //中文$text = mb_substr($str,$i,1,'utf-8');

            $text = $str($i);

            $fontfile = $this->_fontfile;

    imagettftext($this->_image, $size, $angle, $x, $y, $color, $fontfile, $text);

    }

    //雪花,像素和线段

    if ($this->_snow) {

    //使用雪花当作干扰元素

    $this->_getSnow();

    }else{

    if ($this->_pixel) {

    $this->_getPixel();

    }

    if ($this->_line) {

    $this->_getLine();

    }

    }

    //输出图像

    header("Content-type:image/png");

        imagepng($this->_image);

        imagedestroy($this->_image);

        return strtolower($str);

    }

    /**

     * 产生雪花

     * @return [type] [description]

     */

    private function _getSnow(){

    for ($i=1; $i <=$this->_snow ; $i++) { 

    imagestring($this->_image, mt_rand(1,5), mt_rand(0,$this->_width), mt_rand(0,$this->_height), '*', $this->_getRandColor());

    }

    }

    /**

     * 绘制像素

     * @return [type] [description]

     */

    private function _getPixel(){

    for ($i=1; $i < $this->_pixel; $i++) { 

    imagesetpixel($this->_image, mt_rand(0,$this->_width), mt_rand(0,$this->_height), $this->_getRandColor());

    }

    }

    /**

     * 绘制线段

     * @return [type] [description]

     */

    private function _getLine(){

    for ($i=1; $i < $this->_line; $i++) { 

    imageline($this->_image, mt_rand(0,$this->_width), mt_rand(0,$this->_height), mt_rand(0,$this->_width), mt_rand(0,$this->_height), $this->_getRandColor());

    }

    }

    /**

     * 产生验证码字符

     * @param  integer $length 验证码长度

     * @return string 随机字符

     */

    private function _generateStr($length=4){

    if ($length<1 || $length>30) {

    return false;

    }

    $chars = array('a','b','c','d','e','f','g','h','k','m','n','p','x','y','z','A','B','C','D','E','F','G','H','K','M','N','P','X','Y','Z',1,2,3,4,5,6,7,8,9);

    $str = join('',array_rand(array_flip($chars),$length));

    return $str;

    }

    private function _getRandColor(){

    return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));

    }

 }

 ?>


正在回答

4 回答

哥们,构造函数construct前面是两个下划线,这是个大坑!

0 回复 有任何疑惑可以回复我~

你这个应该是new实例化的时候,传进去的参数需要是一个数组,包含fontfile这个元素的数组(你这个报错不是资源,是因为构造里面,在检测字体文件那一步没有通过,没有进去到里面创建画布所致)

0 回复 有任何疑惑可以回复我~
<?php
Class Captcha {
    // 字体文件
    private $_fontFile="";
    // 字体大小
    private $_size=20;
    // 画布宽度
    private $_width=120;
    // 画布高度
    private $_height=40;
    // 验证码长度
    private $_length=5;
    // 画布资源
    private $_image=null;
    private $_snow=0;
    private $_pixel=0;
    private $_line=0;
    public function __construct($config=array()){
        if(is_array($config) && count($config)>0){
            if(isset($config['fontFile']) && is_file($config['fontFile'])&& is_readable($config['fontFile'])){
                $this->_fontFile=$config['fontFile'];
            }
            // 检测是否设置字体大小
            if(isset($config['size']) && $config['size']>0){
                $this->_size=(int)$config['size'];
            }
            // 检测是否设置宽和高
            if(isset($config['width']) && $config['width']>0){
                $this->_width=(int)$config['width'];
            }
            if(isset($config['height']) && $config['height']>0){
                $this->_height=(int)$config['height'];
            }
            // 检测是否设置验证码长度
            if(isset($config['length']) && $config['length']>0){
                $this->_length=(int)$config['length'];
            }

            // 配置干扰元素
            if(isset($config['snow']) && $config['snow']>0){
                $this->_snow=(int)$config['snow'];
            }
            if(isset($config['pixel']) && $config['pixel']>0){
                $this->_pixel=(int)$config['pixel'];
            }
            if(isset($config['line']) && $config['line']>0){
                $this->_line=(int)$config['line'];
            }
            $this->_image=imagecreatetruecolor($this->_width, $this->_height);
            return $this->_image;
        }
    }

    public function getCaptcha(){
        $bgRandCol=imagecolorallocate($this->_image, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
        imagefilledrectangle($this->_image, 0, 0, $this->_width, $this->_height, $bgRandCol);
        $str=$this->_generateStr($this->_length);
        if(false===$str){
            return false;
        }
        $fontFile=$this->_fontFile;
        for ($i=1;$i<$this->_length;$i++){
            $size=$this->_size;
            $angle=mt_rand(-30, 30);
            $x=ceil($this->_width/$this->_length)*$i+mt_rand(5,10);
            $y=ceil($this->_height/1.5);
            $text=$str[$i];
            imagettftext($this->_image, $size, $angle, $x, $y, $this->getRandCol(), $fontFile, $text);
        }
        if($this->_snow>0){
            $this->_getSnow();
        }else{
            if($this->_pixel>0){
                $this->_getPixel();
            }
            if($this->_line>0){
                $this->getLine();
            }
        }
        header('Content-type:image/png');
        imagepng($this->_image);
        imagedestroy($this->_image);
    }

    private function _getSnow(){
        for($i=1;$i<=$this->_snow;$i++){
            imagestring($this->_image, mt_rand(1,5), mt_rand(0, $this->_width), mt_rand(0, $this->_height), '*', $this->getRandCol());
        }
    }

    private function _getPixel(){
        for($i=1;$i<=$this->_pixel;$i++){
            imagesetpixel($this->_image, mt_rand(0, $this->_width), mt_rand(0, $this->_height), $this->getRandCol());
        }
    }

    private function getLine(){
        for($i=1;$i<=$this->_line;$i++){
            imageline($this->_image,mt_rand(0, $this->_width), mt_rand(0, $this->_height),mt_rand(0, $this->_width), mt_rand(0, $this->_height),$this->getRandCol());
        }
    }
    private function getRandCol(){
        return imagecolorallocate($this->_image, mt_rand(10, 100), mt_rand(10, 100), mt_rand(10, 100));
    }

    private function _generateStr($length=4){
        if($length<1 || $length>30){
            return false;
        }
        $chars=array(
            'a','b','c','d','e','f','g','h','k','m','n','p','x','y','z','A','B','C','D','E','F','G','H','K','M','N','P','X','Y','Z', 2,3,4,5,6,7,8,9
        );
        $str=join('',array_rand(array_flip($chars),$length));
        return $str;
    }
}

我这边更改了一些,$_length有个问题,设置的时候要多加一个,要不然设置为4,显示只有3个str

0 回复 有任何疑惑可以回复我~
//看一下我写的,你的里面有好多小错误点,我就不一一列举了
<?php
class Captcha{
	//字体文件
	private $_fontfile = '';
	//画布宽度
	private $_width = 120;
	//画布宽度
	private $_height = 40;
	//字体大小
	private $_size = 20;
	//验证码长度
	private $_length = 4;
	//画布资源
	public $_image = null;
	//干扰元素,雪花数量
	private $_snow = 0;
	private $_pixel = 0;
	private $_line = 0;
	/**
	 * [初始化数据]
	 * @param array $config 
	 */
	public function __construct($config=array()){
		if(is_array($config)&&count($config)>0){
			//检测字体文件是否存在并且可读
			if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){
				$this->_fontfile = $config['fontfile'];
				//检测是否设置画布宽和高
				if(isset($config['width'])&&$config['width']>0){
					$this->_width = (int)$config['width'];
				}
				if(isset($config['height'])&&$config['height']>0){
					$this->_height = (int)$config['height'];
				}
				//检测是否设置字体大小
				if(isset($config['size'])&&$config['size']>0){
					$this->_size = (int)$config['size'];
				}
				//检测是否设置验证码
				if(isset($config['length'])&&$config['length']>0){
					$this->_length = (int)$config['length'];
				}
				//检干扰元素
				if(isset($config['snow'])&&$config['snow']>0){
					$this->_snow = (int)$config['snow'];
				}
				if(isset($config['pixel'])&&$config['pixel']>0){
					$this->_pixel = (int)$config['pixel'];
				}
				if(isset($config['line'])&&$config['line']>0){
					$this->_line = (int)$config['line'];
				}
				$this->_image = imagecreatetruecolor($this->_width, $this->_height);
				return $this->_image;
			}
			else{
				return false;
			}
			
		}
		else{
			return false;
		}
	}
	private function _getRandColor(){
		return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
	}
	/**
	 * 得到验证码
	 * @return [type] [description]
	 */
	public function getCaptcha(){
		$white = imagecolorallocate($this->_image,255,255,255);
		//填充矩形
		imagefilledrectangle($this->_image, 0, 0, $this->_width, $this->_height, $white);
		//生成验证码
		$str = $this->_generateStr($this->_length);
		if(false===$str)
			return false;
		$fontfile = $this->_fontfile;
		//绘制
		for($i=0;$i<$this->_length;$i++){
			$size = $this->_size;
			$angle = mt_rand(-30,30);
			$x = ceil($this->_width/$this->_length)*$i+mt_rand(5,10);
			$y = ceil($this->_height/1.5);
			$color = $this->_getRandColor();
			//$this->不要忘啦写
			// $text = mb_substr($str,$i,i,'utf-8');
			$text = $str{$i};
			//这里是大括号!!!掉坑里了
			imagettftext($this->_image, $size, $angle, $x, $y, $color, $fontfile ,$text);
		}
		//干扰元素
		if($this->_snow){
			$this->_getSnow();
		}else{
			if($this->_pixel){
				$this->_getPixel();
			}
			if($this->_line){
				$this->_getLine();
			}
		}
		//输出图像
		header('content-type:image/png');
		imagepng($this->_image);
		imagedestroy($this->_image);
		return strtolower($str);
	}
	/**
	 * 产生雪花干扰元素
	 * @return [type] [description]
	 */
	private function _getSnow(){
		for($i=1;$i<=$this->_snow;$i++){
			imagestring($this->_image, mt_rand(1,5), mt_rand(0,$this->_width),mt_rand(0,$this->_height),'*',$this->_getRandColor());//第二个参数是字体大小在1-5
		}
	}
	/**
	 * 绘制像素
	 * @return [type] [description]
	 */
	private function _getPixel(){
		for($i=1;$i<=$this->_pixel;$i++){
			imagesetpixel($this->_image, mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor());
		}
	}
	/**
	 * 绘制线段
	 * @return [type] [description]
	 */
	private function _getLine(){
		for($i=1;$i<=$this->_line;$i++){
			imageline($this->_image, mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height), $this->_getRandColor());
		}
	}
	/**
	 * 产生验证码字符
	 * @param  integer $length [验证码长度]
	 * @return 随机字符 string          
	 */
	private function _generateStr($length=4){
		if($length<1 || $length>30){
			return false;
		}
		$chars = array('a','b','c','d','e','f','g','h','i','j','k','m','n','p','x','y','z','A','B','C','D','E','F','G','H','I','J','K','M','N','P','X','Y','Z',1,2,3,4,5,6,7,8,9);
		$str = join('',array_rand(array_flip($chars),$length));
		return $str;
	}
	
}
?>


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为啥我的报错呢?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信