急 求助!!我的类哪里写错了 帮忙指下

guoyingxue 2010-01-20 11:26:09
<?php
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->num=$num;
$this->disnum=$disnum;
$this->strcode=$this->createCode();
$this->iscurve=$iscurve;

}

public function createCode(){
$this->createImage();
$this->createStrCode();
$this->writeCode();
$this->createDisCode();
if($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);
}
}
?>
...全文
88 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
iwantnet 2010-01-21
  • 打赏
  • 举报
回复
真猛呀!
程序猿之殇 2010-01-20
  • 打赏
  • 举报
回复
再修正一次!

<?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();
?>
程序猿之殇 2010-01-20
  • 打赏
  • 举报
回复
代码错误多多,写的不够严谨!
程序猿之殇 2010-01-20
  • 打赏
  • 举报
回复
<?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();
?>
总哈哈 2010-01-20
  • 打赏
  • 举报
回复
火兄猛啊!
苍蝇①号 2010-01-20
  • 打赏
  • 举报
回复
太长。看的眼花

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧