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=''; public function __construct() {
} //创建验证码
public function 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;
} //设置字体文件
public function font($font)
{
$this->font= $font;
return $this;
} //设置文字大小
public function fontSize($fontSize)
{
$this->fontSize=$fontSize;
return $this;
} //设置字体颜色
public function fontColor($fontColor)
{
$this->fontColor = $fontColor;
return $this;
} //验证码数量
public function num($num)
{
$this->codeLen=$num;
return $this;
} //设置宽度
public function width($width)
{
$this->width = $width;
return $this;
} //设置高度
public function height($height)
{
$this->height = $height;
return $this;
} //设置背景颜色
public function background($color)
{
$this->bgColor = $color;
return $this;
} //返回验证码
public function get() {
return $_SESSION['code'];
} //生成验证码
private function 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;
} //建画布
private function create() {
if (!$this->checkGD())
return false;
$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();
} //画线
private function 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);
}
} //画矩形边框
private function createRec() {
//imagerectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->fontColor);
} //写入验证码文字
private function 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;
} //画线
private function 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库
private function checkGD() {
return extension_loaded('gd') && function_exists("imagepng");
} }

php生成验证码类的更多相关文章

  1. PHP常用类------生成验证码类Code

    直接附上代码吧!很简单的代码,写一遍基本就会了,主要明白用GD库画图的几个步骤: 1.先创建画布,不然画在哪儿?(imagecreatetruecolor)2.根据要画的数据,选定颜色 (imagec ...

  2. C#生成验证码类

    using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;us ...

  3. Asp.net mvc生成验证码

    1.生成验证码类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  4. Laravel 下生成验证码的类

    <?php namespace App\Tool\Validate; //验证码类 class ValidateCode { private $charset = 'abcdefghkmnprs ...

  5. JavaWeb学习总结(七):通过Servlet生成验证码及其应用 (BufferedImage类)

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  6. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

  7. THINKPHP源码学习--------验证码类

    TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.p ...

  8. 动态生成验证码———MVC版

    上面有篇博客也是写的验证码,但那个是适用于asp.net网站的. 今天想在MVC中实现验证码功能,弄了好久,最后还是看博友文章解决的,感谢那位博友. 首先引入生成验证码帮助类. ValidateCod ...

  9. java web学习总结(九) -------------------通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  10. PHP生成验证码及单实例应用

    /* note: * this 指向当前对象本身 * self 指向当前类 * parent 指向父类 */ /* 验证码工具类 * @author pandancode * @date 20150- ...

随机推荐

  1. Charles抓包配置、常见问题和解决方法

    1.下载安装charles,官方下载地址:https://www.charlesproxy.com/download/ 如图,下载所对应系统需要的相应版本即可. 2.解压Charles包,双击Char ...

  2. Activity的创建

    Activity的创建: 1.layout内写入相关代码 此处为显示的页面 2.Java内创建相关类写入代码 3.在清单内写入 快捷方法:直接完成上面步骤 layout: match_parent// ...

  3. Hbaseshell命令中的一些语法

    help 'xx' 看库list_namespace 看表list 建表create 't1','f1' 写数据put 't1','r1','c1:name','value' 读数据一行get 't1 ...

  4. docker 容器卷

    创建各种卷 [root@docker ~]# docker volume create mqy-vo101 mqy-vo101 [root@docker ~]# docker inspect mqy- ...

  5. 【JDBC】自定义事务注解实现

    参考自: https://blog.csdn.net/qq_28986619/article/details/94451889 数据源选型,我采用的是C3P0,下面是需要的依赖: <?xml v ...

  6. 【Project】JS的Map对象前后交互问题

    这是我在项目中写的一个Map对象: let map = new Map(); for (let i = 0; i < type_checked_value.length; i++) { let ...

  7. 【JDBC】Extra02 SqlServer-JDBC

    官网驱动获取地址: https://www.microsoft.com/zh-cn/download/details.aspx Maven仓库获取: https://mvnrepository.com ...

  8. PEP 703作者给出的一种no-GIL的实现——python3.9的nogil版本

    PEP 703的内容是什么,意义又是什么呢? 可以说python的官方接受的no-GIL提议的PEP就是PEP 703给出的,如果GIL帧的从python中移除那么可以说对整个python生态圈将有着 ...

  9. [COCI2021-2022#6] Naboj 题解

    前言 题目链接:洛谷. 题意简述 给定一张无向图,每条边有个哨兵,初始在边的中间.你可以把某个结点旁边的哨兵全部吸引或远离这个结点.给出最后每个哨兵在边的哪一端,请构造出一种可能的操作方案或报告无解. ...

  10. TwinCAT3 - 实现自己的Dictionary

    目录 1,前言 2,C#的字典 3,TwinCAT3的字典 定义功能块 添加方法 4,用起来 1,前言 C#有字典,TwinCAT没字典,咋办,自己写一个咯 2,C#的字典 C#的字典使用很简单,下面 ...