PHP 动态随机生成验证码类代码

时间:2024-05-02 17:18:02 

下面是效果图,这个效果图是没有开启干扰码的效果图
PHP 动态随机生成验证码类代码
下面是类代码


<?php
/************************************************
//FILE:ImageCode
//DONE:生成动态验证码类
//DATE"2010-3-31
//Author:www.5dkx.com 5D开心博客
************************************************************************/
class ImageCode{
private $width; //验证码图片宽度
private $height; //验证码图片高度
private $codeNum; //验证码字符个数
private $checkCode; //验证码字符
private $image; //验证码画布
/************************************************************************
// Function:构造函数
// Done:成员属性初始化
// Author:www.5dkx.com 5D开心博客
************************************************************************/
function __construct($width=60,$height=20,$codeNum=4)
{
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->checkCode = $this->createCheckCode();
}
function showImage()
{
$this->getcreateImage();
$this->outputText();
$this->setDisturbColor();
$this->outputImage();
}
function getCheckCode()
{
return $this->chekCode;
}
private function getCreateImage()
{
$this->image = imagecreatetruecolor($this->width,$this->height);
$back = imagecolorallocate($this->image,255,255,255);
$border = imagecolorallocate($this->image,255,255,255);
imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
//使用纯白色填充矩形框,这里用的话后面干扰码失效
/*如果想用干扰码的话使用下面的*/
//imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
}
private function createCheckCode()
{
for($i=0;$i<$this->codeNum;$i++)
{
$number = rand(0,2);
switch($number)
{
case 0: $rand_number = rand(48,57); break;//数字
case 1: $rand_number = rand(65,90);break;//大写字母
case 2: $rand_number = rand(97,122);break;//小写字母
}
$asc = sprintf("%c",$rand_number);
$asc_number = $asc_number.$asc;
}
return $asc_number;
}
private function setDisturbColor()//干扰吗设置
{
for($i=0;$i<=100;$i++)
{
//$color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
$color = imagecolorallocate($this->image,255,255,255);
imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
}
//$color = imagecolorallocate($this->image,0,0,0);
//imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
}
private function outputText()
{
//随机颜色、随机摆放、随机字符串向图像输出
for($i=0;$i<=$this->codeNum;$i++)
{
$bg_color = imagecolorallocate($this->image,rand(0,255),rand(0,128),rand(0,255));
$x = floor($this->width/$this->codeNum)*$i+3;
$y = rand(0,$this->height-15);
imagechar($this->image,5,$x,$y,$this->checkCode[$i],$bg_color);
}
}
private function outputImage()
{
if(imagetypes()&IMG_GIF)
{
header("Content_type:image/gif");
imagegif($this->image);
}
elseif(imagetypes()&IMG_JPG)
{
header("Content-type:image/jpeg");
imagejpeg($this->image,"",0.5);
}
elseif(imagetypes()&IMG_PNG)
{
header("Content-type:image/png");
imagejpeg($this->image);
}
elseif(imagetypes()&IMG_WBMP)
{
header("Content-type:image/vnd.wap.wbmp");
imagejpeg($this->image);
}
else
{
die("PHP不支持图像创建");
}
}
function __destruct()
{
imagedestroy($this->image);
}
}
/*显示*/
/*******************************************************************
session_start();
$image = new ImageCode(60,20,4);
$image->showImage();
$_SESSION['ImageCode'] = $image->getCheckCode();
*******************************************************************/
?>
标签:随机,验证码
0
投稿

猜你喜欢

  • 如何使用python批量修改文本文件编码格式

    2021-02-15 14:01:30
  • 命令行传递参数argparse.ArgumentParser的使用解析

    2023-09-09 12:28:17
  • Python 模块EasyGui详细介绍

    2022-04-27 22:55:39
  • Python reversed反转序列并生成可迭代对象

    2022-07-10 04:51:41
  • python 遍历可迭代对象的实现方法

    2021-03-02 07:13:07
  • 微信小程序picker组件简单用法示例

    2023-07-23 10:49:32
  • 在数据库中自动生成编号的实现方法分享

    2011-11-03 16:55:24
  • Python双精度浮点数运算并分行显示操作示例

    2024-01-01 21:33:10
  • 10款最好的Web开发的 Python 框架

    2023-12-21 11:26:04
  • JavaScript实现模仿桌面窗口的方法

    2024-04-19 10:16:56
  • 在Python的Django框架中调用方法和处理无效变量

    2023-06-12 06:22:19
  • python DataFrame获取行数、列数、索引及第几行第几列的值方法

    2023-08-19 18:12:13
  • python实现爬虫统计学校BBS男女比例之数据处理(三)

    2022-12-11 18:56:45
  • Django forms表单 select下拉框的传值实例

    2023-04-21 02:23:53
  • python搭建服务器实现两个Android客户端间收发消息

    2022-05-24 12:21:12
  • python 破解加密zip文件的密码

    2021-01-10 19:59:39
  • python正则过滤字母、中文、数字及特殊字符方法详解

    2022-02-20 10:14:47
  • 解决ajax+php中文乱码的方法详解

    2024-06-05 09:49:08
  • JavaScript 中断请求几种方案详解

    2024-05-09 10:35:51
  • python+tkinter+mysql做简单数据库查询界面

    2024-01-19 21:13:20
  • asp之家 网络编程 m.aspxhome.com