错误提示
Warning: imagecolorallocate() expects parameter 1 to be resource, boolean given in E:\wampserver\project\验证码类的封装及注册页面的测试\验证码类的封装.php on line 124
Warning: imagettftext() expects parameter 1 to be resource, boolean given in E:\wampserver\project\验证码类的封装及注册页面的测试\验证码类的封装.php on line 85
求大神帮忙指点一下
代码段
class Captcha{
private $_width=120;
private $_height=40;
private $_fontfile='';
private $_size=20;
private $_length=4;
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'];
}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'];
}
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;
}
}
public function getCaptcha(){
$white=imagecolorallocate($this->_image,255,255,255);
$this->_image=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(-15,15);
$x=mt_rand(10,20)+ceil($this->_width/$this->_length)*$i;
$y=mt_rand(($this->_height/2)-5,($this->_height/2)+10);
$color=$this->_getRandColor();
$text=mb_substr($str,$i,1,'utf-8');
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/jpeg');
imagejpeg($this->_image);
imagedestroy($this->_image);
}
private function _generateStr($length=4){
if ($length<1||$length>30){
return false;
}
$chars=array( 'a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','J','K','M','N','P','Q','R','S','T','U','V','W','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));
}
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());
}
}
private function _getPixel(){
for ($i=1;$i<=$this->_plxe;$i++){
imagesetpixel($this->_image, mt_rand(0,$this->_width), mt_rand(0,$this->_height), $this->getRandColor());
}
}
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());
}
}
}
$config=array(
'fontfile'=>'E:\wampserver\project\Groupsex.ttf'
);
$cap=new Captcha($config);
$cap->getCaptcha();