===================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] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  2. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  3. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  4. 【C#】toString("Format") 格式化

    1..ToString("P");//得到小数点后2位的百分比,自动 加上%号;//9512.35% 这个比较厉害! furenjun yoolonet

  5. shell及脚本1——变量

    一.shell shell是操作系统与用户之间的沟通的渠道,可以接收并执行用户的命令,有很多shell程序,目前linux默认使用bash shell程序. bash shell有很多实用功能,例如: ...

  6. ABP文档 :Overall - Module System

    模块介绍 ABP提供了构建模块并将这些模块组合起来创建应用的基础设施.一个模块可以依赖另一个模块.一般来说,一个程序集可以认为是一个模块.如果应用中有多个程序集,建议为每个程序集创建一个模块定义.模块 ...

  7. python基础补漏-03-函数

    函数:一般来说就是 以功能划分的代码模块 [1] 内置函数 一般我们使用的模块 ---可以大概有个了解 大多数的用法都很简单 2 [函数返回值] 我们应该控制函数的每条分支. 也就是说 我们得到的函数 ...

  8. lua命令行编译

    http://jingyan.baidu.com/article/359911f551917457fe0306e5.html 最后将生成的.exe解释器的根目录配置到系统环境变量 copy lua.c ...

  9. css 固定HTML表格的宽度

    在网页中插件表格时,就算你有时定义了宽度,默认的也会根据里面内容的来自动拉伸.有时候自动拉伸是好,但是如果你表格里面的内容太长,表格就会拉伸的特别难看. 像下面的表格,正常的显示应该如下: 但是如果里 ...

  10. oracle Entity db.Database.SqlQuery ORA-01843: 无效的月份

    原因是oracle的日期格式化格式和本地语言环境的日期格式不一致导致的. 一般情景为oralce格式为英文格式 本地服务器或者开发机的环境为中文环境. 使用Dbcontext 实例一般不会有问题. 但 ...