21,886
社区成员
发帖
与我相关
我的任务
分享
<?php
error_reporting(E_ALL & ~E_NOTICE);
class Code{
private $width;//图片宽
private $height;//图片高
private $codenum;//验证码字符数
private $disnum;//干扰元素个数
private $image;//画布
private $strcode;//验证码字符串
private $iscurve;//是否开启曲线
function __construct($width=80,$height=30,$num=4,$disnum=100,$iscurve=1){
$this->width=$width;
$this->height=$height;
$this->codenum=$num;
$this->disnum=$disnum;
$this->iscurve=$iscurve;
}
public function createCode(){
$this->createImage();
$this->strcode = $this->createStrCode();
$this->writeCode();
$this->createDisCode();
if($this->iscurve){
$this->createDisCurve();
}
$this->imageType();
}
public function outCode(){
return $this->strcode;
}
private function createImage(){//创建画布
$this->image = imagecreatetruecolor($this->width, $this->height);
$bgcolor = imagecolorallocate($this->image, 255, 255, 255);
$reccolor = imagecolorallocate($this->image, 0, 0, 0);
imagefill($this->image, 0, 0, $bgcolor);
imagerectangle($this->image, 1, 1, $this->width-2, $this->height-2, $reccolor);
}
private function createStrCode(){//生成字符串验证码
$text="";
for($i=0; $i < $this->codenum; $i++){
switch(rand(0, 2)){
case 0:
$text.= sprintf("%c", rand(48,57));
break;
case 1:
$text.= sprintf("%c", rand(65, 90));
break;
case 2:
$text.= sprintf("%c", rand(97, 122));
break;
}
}
return $text;
}
private function writeCode(){ //把字符串写入图片中
for($i=0;$i <$this->codenum;$i++){
$col=imagecolorallocate($this->image,rand(0,255),rand(100,255),rand(0,255));
$x=floor($this->width/$this->codenum)*$i+rand(2,10);
$y=rand(0,$this->height-15);
imagechar($this->image,2,$x,$y,$this->strcode[$i],$col);
}
}
private function createDisCode(){//生成干扰元素
for($i=0;$i <$disnum;$i++){
$col=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($image,rand(0,$this->width-2),rand(0,$this->height-2),$col);
}
}
private function createDisCurve(){//生成干扰曲线
for($i=0; $i <3; $i++){
$curvecolor = imagecolorallocate($this->image, rand(180, 200), rand(180, 200), rand(180, 200));
imagearc($this->image, rand(0, $this->width), rand(0, $this->height), rand($this->width, 2*$this->width), rand($this->height, 2*$this->height), rand(0, 90), rand(180, 360), $curvecolor);
}
}
private function imageType(){
if(imagetypes() & IMG_GIF){
header("content-type:images/gif");
imagegif($this->image);
}elseif(imagetypes() & IMG_JPG){
header("content-type:images/jpeg");
imagejpeg($this->image);
}elseif(imagetypes() & IMG_PNG){
header("content-type:images/png");
imagepng($this->image);
}elseif(imagetypes() & IMG_WBMP){
header("content-type:images/vnd.wap.wbmp");
imagewbmp($this->image);
}else{
die("此格式不支持");
}
}
function __destruct(){//销毁图片
imagedestroy($this->image);
}
}
$code = new Code();
$code->createCode();
?>
<?php
error_reporting(E_ALL & ~E_NOTICE);
class Code{
private $width;//图片宽
private $height;//图片高
private $codenum;//验证码字符数
private $disnum;//干扰元素个数
private $image;//画布
private $strcode;//验证码字符串
private $iscurve;//是否开启曲线
function __construct($width=80,$height=30,$num=4,$disnum=100,$iscurve=1){
$this->width=$width;
$this->height=$height;
$this->codenum=$num;
$this->disnum=$disnum;
$this->iscurve=$iscurve;
$this->strcode=$this->createCode();
}
public function createCode(){
$this->createImage();
$this->strcode = $this->createStrCode();
$this->writeCode();
$this->createDisCode();
if($this->iscurve){
$this->createDisCurve();
}
$this->imageType();
}
public function outCode(){
return $this->strcode;
}
private function createImage(){//创建画布
$this->image = imagecreatetruecolor($this->width, $this->height);
$bgcolor = imagecolorallocate($this->image, 255, 255, 255);
$reccolor = imagecolorallocate($this->image, 0, 0, 0);
imagefill($this->image, 0, 0, $bgcolor);
imagerectangle($this->image, 1, 1, $this->width-2, $this->height-2, $reccolor);
}
private function createStrCode(){//生成字符串验证码
$text="";
for($i=0; $i < $this->codenum; $i++){
switch(rand(0, 2)){
case 0:
$text.= sprintf("%c", rand(48,57));
break;
case 1:
$text.= sprintf("%c", rand(65, 90));
break;
case 2:
$text.= sprintf("%c", rand(97, 122));
break;
}
}
return $text;
}
private function writeCode(){ //把字符串写入图片中
for($i=0;$i <$this->codenum;$i++){
$col=imagecolorallocate($this->image,rand(0,255),rand(100,255),rand(0,255));
$x=floor($this->width/$this->codenum)*$i+rand(2,10);
$y=rand(0,$this->height-15);
imagechar($this->image,2,$x,$y,$this->strcode[$i],$col);
}
}
private function createDisCode(){//生成干扰元素
for($i=0;$i <$disnum;$i++){
$col=imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($image,rand(0,$this->width-2),rand(0,$this->height-2),$col);
}
}
private function createDisCurve(){//生成干扰曲线
for($i=0; $i <3; $i++){
$curvecolor = imagecolorallocate($this->image, rand(180, 200), rand(180, 200), rand(180, 200));
imagearc($this->image, rand(0, $this->width), rand(0, $this->height), rand($this->width, 2*$this->width), rand($this->height, 2*$this->height), rand(0, 90), rand(180, 360), $curvecolor);
}
}
private function imageType(){
if(imagetypes() & IMG_GIF){
header("content-type:images/gif");
imagegif($this->image);
}elseif(imagetypes() & IMG_JPG){
header("content-type:images/jpeg");
imagejpeg($this->image);
}elseif(imagetypes() & IMG_PNG){
header("content-type:images/png");
imagepng($this->image);
}elseif(imagetypes() & IMG_WBMP){
header("content-type:images/vnd.wap.wbmp");
imagewbmp($this->image);
}else{
die("此格式不支持");
}
}
function __destruct(){//销毁图片
imagedestroy($this->image);
}
}
$code = new Code();
$code->createCode();
?>