為啥我的報(bào)錯(cuò)呢?

<?php
?Class Captcha{
//字體文件
private $_fontfile ='';
//畫布寬度和高度
private $_width =120;
private $_height =40;
? ? //字體大小
? ? private $_size =20;
? ? //驗(yàn)證嗎長(zhǎng)度
? ? private $_length =4;
? ? //畫布資源
? ? private $_image =null;
? ? //干擾元素
? ? //雪花*個(gè)數(shù)
? ? private $_snow =0;
? ? //像素個(gè)數(shù)
? ? private $_pixel =0;
? ? //線段個(gè)數(shù)
? ? private $_line =0;
? ? /**
? ? ?* 初始化數(shù)據(jù)
? ? ?* @param ?array ?$config?
? ? ?*/
public function _construct($config=array()){
if(is_array($config)&&count($config)>0){
//檢測(cè)字體是否存在并且可讀
if (isset($config['fontfile'])&&is_file('fontfile')&&is_readable('fontfile')) {
$this->_fontfile = $config['fontfile'];
}else{
return false;
}
//檢測(cè)是否設(shè)置畫布寬和高
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'];
? ?}
? ?//檢測(cè)是否設(shè)置驗(yàn)證碼長(zhǎng)度
? ?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;
? ?}
? ? }
? ? /**
? ? ?* 得到驗(yàn)證碼
? ? ?* @return [type] [description]
? ? ?*/
? ? public function getCaptcha(){
? ? $white = imagecolorallocate($this->_image, 255,255,255);
? ? //填充矩形
? ? imagefilledrectangle($this->_image, 0, 0, $this->_width, $this->_height,$white);
? ? //生成驗(yàn)證碼
? ? $str = $this->_generateStr($this->_length);
? ? if (false===$str) {
? ? return false;
? ? }
? ? //繪制驗(yàn)證碼
? ? 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) {
? ? //使用雪花當(dāng)作干擾元素
? ? $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);
? ? }
? ? /**
? ? ?* 產(chǎn)生雪花
? ? ?* @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());
? ? }
? ? }
? ? /**
? ? ?* 產(chǎn)生驗(yàn)證碼字符
? ? ?* @param ?integer $length 驗(yàn)證碼長(zhǎng)度
? ? ?* @return string 隨機(jī)字符
? ? ?*/
? ? 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));
? ? }
?}
??>
2019-01-05
哥們,構(gòu)造函數(shù)construct前面是兩個(gè)下劃線,這是個(gè)大坑!
2018-04-28
你這個(gè)應(yīng)該是new實(shí)例化的時(shí)候,傳進(jìn)去的參數(shù)需要是一個(gè)數(shù)組,包含fontfile這個(gè)元素的數(shù)組(你這個(gè)報(bào)錯(cuò)不是資源,是因?yàn)闃?gòu)造里面,在檢測(cè)字體文件那一步?jīng)]有通過(guò),沒(méi)有進(jìn)去到里面創(chuàng)建畫布所致)
2017-10-24
<?php Class?Captcha?{ ????//?字體文件 ????private?$_fontFile=""; ????//?字體大小 ????private?$_size=20; ????//?畫布寬度 ????private?$_width=120; ????//?畫布高度 ????private?$_height=40; ????//?驗(yàn)證碼長(zhǎng)度 ????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']; ????????????} ????????????//?檢測(cè)是否設(shè)置字體大小 ????????????if(isset($config['size'])?&&?$config['size']>0){ ????????????????$this->_size=(int)$config['size']; ????????????} ????????????//?檢測(cè)是否設(shè)置寬和高 ????????????if(isset($config['width'])?&&?$config['width']>0){ ????????????????$this->_width=(int)$config['width']; ????????????} ????????????if(isset($config['height'])?&&?$config['height']>0){ ????????????????$this->_height=(int)$config['height']; ????????????} ????????????//?檢測(cè)是否設(shè)置驗(yàn)證碼長(zhǎng)度 ????????????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有個(gè)問(wèn)題,設(shè)置的時(shí)候要多加一個(gè),要不然設(shè)置為4,顯示只有3個(gè)str
2017-10-08
//看一下我寫的,你的里面有好多小錯(cuò)誤點(diǎn),我就不一一列舉了 <?php class?Captcha{ //字體文件 private?$_fontfile?=?''; //畫布寬度 private?$_width?=?120; //畫布寬度 private?$_height?=?40; //字體大小 private?$_size?=?20; //驗(yàn)證碼長(zhǎng)度 private?$_length?=?4; //畫布資源 public?$_image?=?null; //干擾元素,雪花數(shù)量 private?$_snow?=?0; private?$_pixel?=?0; private?$_line?=?0; /** ?*?[初始化數(shù)據(jù)] ?*?@param?array?$config? ?*/ public?function?__construct($config=array()){ if(is_array($config)&&count($config)>0){ //檢測(cè)字體文件是否存在并且可讀 if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){ $this->_fontfile?=?$config['fontfile']; //檢測(cè)是否設(shè)置畫布寬和高 if(isset($config['width'])&&$config['width']>0){ $this->_width?=?(int)$config['width']; } if(isset($config['height'])&&$config['height']>0){ $this->_height?=?(int)$config['height']; } //檢測(cè)是否設(shè)置字體大小 if(isset($config['size'])&&$config['size']>0){ $this->_size?=?(int)$config['size']; } //檢測(cè)是否設(shè)置驗(yàn)證碼 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)); } /** ?*?得到驗(yàn)證碼 ?*?@return?[type]?[description] ?*/ public?function?getCaptcha(){ $white?=?imagecolorallocate($this->_image,255,255,255); //填充矩形 imagefilledrectangle($this->_image,?0,?0,?$this->_width,?$this->_height,?$white); //生成驗(yàn)證碼 $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}; //這里是大括號(hào)!?。〉艨永锪? 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); } /** ?*?產(chǎn)生雪花干擾元素 ?*?@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());//第二個(gè)參數(shù)是字體大小在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()); } } /** ?*?產(chǎn)生驗(yàn)證碼字符 ?*?@param??integer?$length?[驗(yàn)證碼長(zhǎng)度] ?*?@return?隨機(jī)字符?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; } } ?>