php生成验证码类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php生成验证码类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4129字,纯文字阅读大概需要6分钟。
内容图文
php生成验证码类
直接看代码
<?php session_start(); class Code{ //资源private$img; //画布宽度private$width=100; //画布高度private$height=30; //背景颜色private$bgColor=‘#ffffff‘; //验证码private$code; //验证码的随机种子private$codeStr=‘23456789abcdefghjkmnpqrstuvwsyz‘; //验证码长度private$codeLen=4; //验证码字体private$font; //验证码字体大小private$fontSize=16; //验证码字体颜色private$fontColor=‘‘; publicfunction __construct() { } //创建验证码publicfunction make() { if(empty($this->font)) { $this->font = base_path().‘\public\out\consola.ttf‘; } $this->create();//生成验证码header("Content-type:image/png"); imagepng($this->img); imagedestroy($this->img); exit; } //设置字体文件publicfunction font($font) { $this->font= $font; return$this; } //设置文字大小publicfunction fontSize($fontSize) { $this->fontSize=$fontSize; return$this; } //设置字体颜色publicfunction fontColor($fontColor) { $this->fontColor = $fontColor; return$this; } //验证码数量publicfunction num($num) { $this->codeLen=$num; return$this; } //设置宽度publicfunction width($width) { $this->width = $width; return$this; } //设置高度publicfunction height($height) { $this->height = $height; return$this; } //设置背景颜色publicfunction background($color) { $this->bgColor = $color; return$this; } //返回验证码publicfunction get() { return$_SESSION[‘code‘]; } //生成验证码privatefunction createCode() { $code = ‘‘; for ($i = 0; $i < $this->codeLen; $i++) { $code .= $this->codeStr [mt_rand(0, strlen($this->codeStr) - 1)]; } $this->code = strtoupper($code); $_SESSION[‘code‘] = $this->code; } //建画布privatefunction create() { if (!$this->checkGD()) returnfalse; $w = $this->width; $h = $this->height; $bgColor = $this->bgColor; $img = imagecreatetruecolor($w, $h); $bgColor = imagecolorallocate($img, hexdec(substr($bgColor, 1, 2)), hexdec(substr($bgColor, 3, 2)), hexdec(substr($bgColor, 5, 2))); imagefill($img, 0, 0, $bgColor); $this->img = $img; $this->createLine(); $this->createFont(); $this->createPix(); $this->createRec(); } //画线privatefunction createLine(){ $w = $this->width; $h = $this->height; $line_color = "#dcdcdc"; $color = imagecolorallocate($this->img, hexdec(substr($line_color, 1, 2)), hexdec(substr($line_color, 3, 2)), hexdec(substr($line_color, 5, 2))); $l = $h/5; for($i=1;$i<$l;$i++){ $step =$i*5; imageline($this->img, 0, $step, $w,$step, $color); } $l= $w/10; for($i=1;$i<$l;$i++){ $step =$i*10; imageline($this->img, $step, 0, $step,$h, $color); } } //画矩形边框privatefunction createRec() { //imagerectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->fontColor); } //写入验证码文字privatefunction createFont() { $this->createCode(); $color = $this->fontColor; if (!empty($color)) { $fontColor = imagecolorallocate($this->img, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2))); } $x = ($this->width - 10) / $this->codeLen; for ($i = 0; $i < $this->codeLen; $i++) { if (empty($color)) { $fontColor = imagecolorallocate($this->img, mt_rand(50, 155), mt_rand(50, 155), mt_rand(50, 155)); } imagettftext($this->img, $this->fontSize, mt_rand(- 30, 30), $x * $i + mt_rand(6, 10), mt_rand($this->height / 1.3, $this->height - 5), $fontColor, $this->font, $this->code [$i]); } $this->fontColor = $fontColor; } //画线privatefunction createPix() { $pix_color = $this->fontColor; for ($i = 0; $i < 50; $i++) { imagesetpixel($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color); } for ($i = 0; $i < 2; $i++) { imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color); } //画圆弧for ($i = 0; $i < 1; $i++) { // 设置画线宽度 imagearc($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height) , mt_rand(0, 160), mt_rand(0, 200), $pix_color); } imagesetthickness($this->img, 1); } //验证GD库privatefunction checkGD() { returnextension_loaded(‘gd‘) && function_exists("imagepng"); } }
原文:https://www.cnblogs.com/crazytata/p/10171414.html
内容总结
以上是互联网集市为您收集整理的php生成验证码类全部内容,希望文章能够帮你解决php生成验证码类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。