php图像验证码生成代码(2)

<?php class TestCode{//创建类名为TestCode private $width; private $height; private $str; private $im; private $strColor; function __construct($width,$height){ $this->width=$width; $this->height=$height; $this->str=$_GET['code']; $this->createImage(); } function createImage(){ $this->im=imagecreate($this->width,$this->height);//创建画布 imagecolorallocate($this->im,200,200,200);//为画布添加颜色 for($i=0;$i<4;$i++){//循环输出四个数字 $this->strColor=imagecolorallocate($this->im,rand(0,100),rand(0,100),rand(0,100)); imagestring($this->im,rand(3,5),$this->width/4*$i+rand(5,10),rand(2,5),$this->str[$i],$this->strColor); } for($i=0;$i<200;$i++){//循环输出200个像素点 $this->strColor=imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($this->im,rand(0,$this->width),rand(0,$this->height),$this->strColor); } } function show(){// header('content-type:image/png');//定义输出为图像类型 imagepng($this->im);//生成图像 imagedestroy($this->im);//销毁图像释放内存 } } $image=new TestCode(80,20);//将类实例化为对象 $image->show();//调用函数 ?>

二、运行结果

php图像验证码生成代码

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/29e95649531de180fb974261334b812c.html