不顯示驗(yàn)證碼,只有一個(gè)白色的方塊
<?php
Class Captcha{
//字體文件
private $_fontfile='';
//字體大小
private $_size=20;
//寬度
private $_width=200;
//高度
private $_height=50;
//驗(yàn)證碼長度
private $_length=4;
//畫布資源
private $_image=null;
//干擾元素
//雪花*的個(gè)數(shù)
private $_snow=0;
//像素個(gè)數(shù)
private $_pixel=0;
//線段個(gè)數(shù)
private $_line=0;
? ? //初始化數(shù)據(jù)
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;
? ? }
? ? //檢測是否設(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'];
? ? }
? ? //檢測是否設(shè)置字體大小
? ? if(isset($config['size'])&&$config['size']>0){
? ? $this->_size=(int)$config['size'];
? ? }
? ? //檢測是否設(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;
}
}
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($str==false){
return false;
}
$fontfile=$this->_fontfile;
//繪制驗(yàn)證碼
for($i=0;$i<$this->_length;$i++){
$size=$this->_size;
$angle=mt_rand(-15,15);
$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};
imagettftext($this->_image,$size, $angle, $x, $y, $color, $fontfile, $text);
}
//產(chǎn)生干擾元素
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);
}
? ? ? ? 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->_pixel;$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());
}
}
? ?
//產(chǎn)生驗(yàn)證碼字符
private function _generateStr($length=4){
if($length<1 || $length>10){
return false;
}
$chars=array(
'a','b','c','d','e','f','g','h','k','m','n','p','x','y',
'A','B','C','D','E','F','G','H','K','M','N','P','X','Y',
1,2,3,4,5,6,7,8,9
? ? ? ? ? ? );
$str=join('',array_rand(array_flip($chars),$length));
return $str;
}
//產(chǎn)生隨機(jī)顏色
private function _getRandColor(){
? ? ?return imagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
}
}
2022-12-29
字體問題!好好的檢查 一下
2021-01-15
一樣一樣