===================VerifyTool======================

<?php

class VerifyTool
{
private $fontPath; //字体路径
private $verifyStr; //字符库
private $verifyLen; //字符数
private $verifyCode; //验证码
private $verifyImg; //验证图像 public function __construct($fontPath)
{
$this->fontPath = $fontPath;
$this->verifyLen = 4;
$this->verifyStr = '0123456789'
. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
. 'abcdefghijklmnopqrstuvwxyz';
} /**
* 设置验证码所包含的字符
* @param $str
*/
public function setChars($str)
{
$this->verifyStr = $str;
} /**
* 设置验证码字符串长度
* @param $num
*/
public function setLength($num)
{
$this->verifyLen = $num;
} /**
* 得到验证码的内容(默认转换为小写)
* @param bool $tolower
* @return string
*/
public function getCode($tolower = true)
{
if ($tolower) {
return strtolower($this->verifyCode);
} else {
return $this->verifyCode;
}
} /**
* 绘制验证码图像
* @return $this
*/
public function drawImage()
{
$width = $this->verifyLen * 50;//图片宽度
$height = 60;//图片高度
$graylevel = 240;//背景灰度
$fontsize = 24;//字体大小
$content = '';//验证码内容
$image = imagecreatetruecolor($width, $height);
$bgcolor = imagecolorallocate($image, $graylevel, $graylevel, $graylevel);
imagefill($image, 0, 0, $bgcolor);
//绘制随机字符
for ($i = 0; $i < $this->verifyLen; $i++) {
$fontcolor = imagecolorallocate($image, rand(120, 220), rand(60, 150), rand(100, 200));
$fontchar = mb_substr($this->verifyStr, rand(0, mb_strlen($this->verifyStr, 'utf8') - 1), 1, 'utf8');
$x = ($i * $width / $this->verifyLen) + rand(10, 20);
$y = $height / 2 + rand(-5, 5) + $fontsize / 2;
imagettftext($image, $fontsize, rand(-60, 60), $x, $y, $fontcolor, $this->fontPath, $fontchar);
$content .= $fontchar;
}
//绘制随机点
for ($i = 0; $i < $this->verifyLen * 10; $i++) {
$pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
imagefilledellipse($image, rand(0, $width), rand(0, $height), 5, 5, $pointcolor);
}
//绘制随机直线
for ($i = 0; $i < $this->verifyLen; $i++) {
$linecolor = imagecolorallocatealpha($image, rand(60, 120), rand(80, 160), rand(60, 120), rand(80, 100));
imageline($image, 0, rand(0, $height - 0), $width, rand(0, $height - 0), $linecolor);
}
$this->verifyCode = $content;
$this->verifyImg = $image;
return $this;
} /**
* 显示验证码
*/
public function show()
{
header('content-type: image/png');
imagepng($this->verifyImg);
imagedestroy($this->verifyImg);
}
}

VerifyTool.class.php

==================使用方式====================

创建一个verify_image.php文件:

<?php
//开启SESSION
session_start();
//引入验证码工具
require_once 'VerifyTool.class.php';
//初始化工具(必须传入有效的字体路径)
$verifyTool = new VerifyTool('../res/simhei.ttf');
//绘制图像并显示
$verifyTool->drawImage()->show();
//将验证码信息保存至SESSION
$_SESSION['verify_code'] = $verifyTool->getCode();

如果直接打开效果如下:

还可以为验证工具设置参数:

$verifyTool = new VerifyTool('../res/simhei.ttf');
//设置验证码字符内容
$verifyTool->setChars('巧学巧用');
//设置验证码长度
$verifyTool->setLength(10);
$verifyTool->drawImage()->show();

设置参数后效果如下:

==================验证方式====================

创建一个 verify_test.php 文件:

<?php
//如果需要使用SESSION,必须在脚本开始处开启SESSION
session_start();
?>
<!--显示验证码和输入框-->
<img src="verify_image.php">
<form action="verify_test.php" method="get">
<input type="text" name="mycode">
<input type="submit" value="submit">
</form> <?php
if (!empty($_GET['mycode'])) {
//去除用户输入的多余空格,并将输入转换为小写字母
$mycode = strtolower(trim($_GET['mycode']));
//将SESSION保存的验证码信息取出,并进行比较
if ($mycode == $_SESSION['verify_code']) {
echo '验证成功,请继续';
} else {
echo '验证失败,请重试';
}
}

这仅仅是测试验证码的小Demo,如果要在其他场景下使用的话,就需要理解其中的原理,做到巧学巧用才行。

PHP 验证码生成类(可定制长度和内容)的更多相关文章

  1. Python实现网站注册验证码生成类

    # -*- coding:utf-8 -*- ''' Created on 2017年4月7日 @author: Water ''' import os import random import st ...

  2. 开发工具类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等

    以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 六位图片验证码生成:包括纯数字.小写字母.大写字母.大小写混合.数 ...

  3. JAVA 验证码生成(转)

    最近做了一下验证码的功能,网上找了一篇还不错,引用下:http://blog.csdn.net/ruixue0117/article/details/22829557 这篇文章非常好,但是web和js ...

  4. java web中验证码生成的demo

    首先创建一个CaptailCode类 package com.xiaoqiang.code; import java.awt.*; import java.awt.font.FontRenderCon ...

  5. JavaWeb开发之普通图片验证码生成技术与算术表达式验证码生成技术

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6134649.html    另:算术验证码生成的JSP.Servlet实现均已移植github:https:/ ...

  6. web页面 验证码 生成

    web页面 验证码 生成 kaptcha 是一个非常实用的验证码生成工具.有了它,你可以生成各种样式的验证码,因为它是可配置的.kaptcha工作的原理是调用 com.google.code.kapt ...

  7. C# Json反序列化 C# 实现表单的自动化测试<通过程序控制一个网页> 验证码处理类:UnCodebase.cs + BauDuAi 读取验证码的值(并非好的解决方案) 大话设计模式:原型模式 C# 深浅复制 MemberwiseClone

    C# Json反序列化   Json反序列化有两种方式[本人],一种是生成实体的,方便处理大量数据,复杂度稍高,一种是用匿名类写,方便读取数据,较为简单. 使用了Newtonsoft.Json,可以自 ...

  8. ajax原理,验证码生成原理

    什么是ajax AJAX:”Asynchronous JavaScript and XML” 中文意思:异步JavaScript和XML 指一种创建交互式网页应用的网页开发技术.   不是指一种单一的 ...

  9. 轻量级验证码生成插件webutil-licenseImage

    轻量级验证码生成插件webutil-licenseImage源码与实例应用   webutil-licenseImage 插件内置4种验证码样式,支持用户扩展.自定义样式实现简单验证码. 源码脱管地址 ...

随机推荐

  1. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  2. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  3. [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  4. ASP.NET - Web API,从简单类型到复杂类型的参数传递用例,以及传递简单string类型的解决办法

    一,简单类型的传值 比如 public Users Get(int id) ,它可以使用两种方式获取: api/default/ $.get("/api/default",{id: ...

  5. 百度地图demo

    以下代码拷贝成html,直接运行即能看到百度地图 <!DOCTYPE html><html> <head> <meta http-equiv="Co ...

  6. mysql函数大全

    对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符的ASCII代码值.如果str是空字符串,返回0.如果str是NULL,返回NULL. mysql& ...

  7. 让Visual Studio 2013为你自动生成XML反序列化的类

    Visual Sutdio 2013增加了许多新功能,其中很多都直接提高了对代码编辑的便利性.如: 1. 在代码编辑界面的右侧滚动条上显示不同颜色的标签,让开发人员可以对所编辑文档的修改.查找.定位情 ...

  8. Linux工具入门:make工具与Makefile文件

    1. make工具 利用make工具可以自动完成编译工作,这些工作包括: 如果修改了某几个源文件,则只重新编译这几个源文件 如果某个头文件被修改了,则重新编译所有包含该头文件的源文件 利用这种自动编译 ...

  9. .net 开源组件

    文章转自:http://www.cnblogs.com/asxinyu/p/dotnet_opensource_project_3.html   在前2篇文章这些.NET开源项目你知道吗?让.NET开 ...

  10. 解决virtualbox装ghost xp装驱动时报portcls.sys蓝屏的问题

    原因:portcls.sys是声卡驱动文件,驱动与模拟的声卡型号不匹配. 解决办法:进入PE,把C:\sysprep\audio目录删除.进入系统后用驱动精灵装驱动.