php极验证,PHP做验证码

热度311℃

//PHP验证码

class Code{

//图像资源

private $img;

//宽度

private $width;

//高度

private $height;

//背景颜色

private $bgColor;

//字体大小

private $fontSize;

//验证码长度

private $codeLen;

//字体文件

private $fontFile;

//验证码种子

private $seed;

public function __construct($width=150,$height=40,$codeLen=4,$fontSize=8,$bgColor='#ffffff',$seed='1234567890qwertyuiopasdklzxcvbnm'){

//宽度

$this->width = $width;

//高度

$this->height = $height;

//背景色

$this->bgColor = $bgColor;

//字体大小

$this->fontSize = $fontSize;

//验证码长度

$this->codeLen = $codeLen;

//字体文件

$this->fontFile = "./font.ttf";

//种子

$this->seed = $seed;

}

/**

* 显示验证码

*/

public function show(){

//1.发送头部

header('Content-type:image/png');

//2.创建画布,填充画布

$this->createBg();

//3.写字

$this->write();

//4.干扰

$this->makeTrouble();

//5.输出

imagepng($this->img);

//6.销毁

imagedestroy($this->img);

}

/**

* 创建干扰

*/

private function makeTrouble(){

for ($i=0; $i < 10; $i++) {

//随机颜色

$color = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

//线

imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width),mt_rand(0, $this->height), $color);

}

}

/**

* 创建画布

*/

private function createBg(){

$img = imagecreatetruecolor($this->width, $this->height);

//把16进制颜色(#ffffff)转为10进制颜色(能被imagefill使用)

$bgColor = hexdec($this->bgColor);

imagefill($img, 0, 0, $bgColor);

$this->img = $img;

}

/**

* 写字

*/

private function write(){

for ($i=0; $i < $this->codeLen; $i++) {

//x坐标

$x = $i * ($this->width / $this->codeLen) + 10;

$y = ($this->height + $this->fontSize) / 2;

//随机颜色

$color = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));

//具体字

$text = $this->seed[mt_rand(0, strlen($this->seed) - 1)];

//写字

imagettftext($this->img, $this->fontSize, mt_rand(-45, 45), $x, $y, $color, $this->fontFile, $text);

}

}

}

//显示验证码

//第一个参数传验证码宽度 第二个参数传验证码的高度

//第三个参数传验证码个数 第四个传验证码字体大小

//第五个参数传字体颜色 第六个传验证码种子

//参数不传就是默认

$code = new Code(150,40,4,30);

$code->show();

?>

字体文件需要换成自己的

相关评论(0)

a7b81af59ffd0a4146ebdfcaefc4545c.png

发布评论

526d68976081f3dbf994a1bd9fce2f5e.png

您是不是忘了说点什么?

提 交

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值