php图像验证码生成代码
作者:cakin24
这篇文章主要为大家详细
介绍了php图像验证码的生成代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
介绍了php图像验证码的生成代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了php封装的一个生成图像验证码,供大家参考,具体内容如下
一、代码
index..php
TestCode.php
<?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();//调用函数
?> 二、运行结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
