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. SQL提高查询性能的几种方式

    创建索引,提高性能 索引可以极大地提高查询性能,其背后的原理: 索引是的数据库引擎能够快速的找到表中的数据,它们类似于书籍的目录,使得你不需要逐页查找所需要的信息 索引能够帮助数据库引擎直接定位到所需 ...

  2. 【MySQL】java.sql.SQLException: Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT) for operation '='

    问题原因参考: http://t.zoukankan.com/zhulei2-p-13451554.html collations 排序规则 Illegal mix 非法混合 SQL报错指出,操作符等 ...

  3. 【RabbitMQ】03 订阅模式

    Pub / Sub 订阅模式 特点是 一条消息可以给多个消费者接收了 首先创建订阅模式生产者发生一些代码变动: package cn.dzz.pubSub; import com.rabbitmq.c ...

  4. 从.net开发做到云原生运维(四)——.net core的微服务开发

    1. .net 6.0项目模板变更 在.net 5和.net 3.1的时候,asp.net core项目模板里有个Program类和Startup类,在.net 6中引入了一个最小api的项目模板,在 ...

  5. python性能分析器:cProfile

    代码: (1) import cProfile import re cProfile.run('re.compile("foo|bar")') 运行结果: (2) import c ...

  6. 【转载】 Makefile的静态模式%.o : %.c

    版权声明:本文为CSDN博主「猪哥-嵌入式」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/u012351051 ...

  7. js 实现俄罗斯方块(三)

    我又来啦!上一篇有点水,本篇我们来干货! 嘿嘿,首先我们先搭建游戏世界------网格 所有的操作包括左移右移下移旋转都是在这个网格中 既然是使用js来写当然跑不了html啦,实现网格最简单的 方法就 ...

  8. 5分钟教你使用idea调试SeaTunnel自定义插件

    在用Apache SeaTunnel研发SM2加密组件过程中,发现社区关于本地调试SeaTunnel文章过于简单,很多情况没有说明,于是根据自己遇到问题总结这篇文档.SeaTunnel本地调试官方文档 ...

  9. mybatis打印sql 转

    我们在使用mybatis开发过程中,经常需要打印sql以及输入输出,下面说一下mybatis结合log4j打印sql的. 1.添加mybatis配置 mybatis的日志打印方式比较多,SLF4J | ...

  10. 强!34.1K star! 再见Postman,新一代API测试利器,功能强大、颜值爆表!

    1.引言 在当今的互联网时代,API(应用程序编程接口)已经成为连接不同软件系统的桥梁.作为一名开发者,掌握API测试技能至关重要.市面上的API测试工具琳琅满目,今天我们要介绍的是一款开源.跨平台的 ...