PHP生成图片验证码demo【OOP面向对象版本】
下面是我今天下午用PHP写的一个生成图片验证码demo,仅供参考。
这个demo总共分为4个文件,具体代码如下:
1、code.html中的代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>登录、注册验证码生成</title>
</head>
<body>
<!--
* @Description 网站登录/注册验证码生成类
* @Author 赵一鸣
* @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
* @Date 2016年10月6日
-->
<form action="checkcode.php" method="post">
<input type="text" name="code" /><br/>
<img src="showcode.php" onclick="this.setAttribute('src','showcode.php?'+Math.random())" />
<span>看不清?点击图片即可切换验证码</span><br/>
<input type="submit" name="sub" value="登录/注册" />
</form>
</body>
</html>
2、createcode.class.php中的代码:
<?php
/**
* @Description 网站登录/注册验证码生成类
* @Author 赵一鸣
* @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
* @Date 2016年10月6日
*/
class Createcode{
//画布资源
public $img;
//画布宽度
private $img_width;
//画布高度
private $img_height;
//画布颜色
private $img_bgcolor;
//验证码文字内容
private $str_content;
//生成的验证码内容
private $code_content;
//验证码颜色
private $code_content_color;
//构造函数
public function __construct($img_width,$img_height,$str_content,$code_content_color){
if($this->gdcheck()){
$this->img_width = $img_width;
$this->img_height = $img_height;
$this->str_content = $str_content;
$this->code_content_color = $code_content_color;
$this->get_code();
$this->session_code();
}
}
//生成画布
public function get_img(){
//定义画布
$this->img = imagecreatetruecolor($this->img_width, $this->img_height);
//画布背景色
$this->img_bgcolor = imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
//给画图填充背景色
imagefill($this->img, 0, 0, $this->img_bgcolor);
//取得画布的宽高
$img_width = imagesx($this->img);
$img_height = imagesy($this->img);
//画布中插入验证码
imagestring($this->img, 5, ($this->img_width/3), ($this->img_height/2.5), $this->code_content, imagecolorallocate($this->img, hexdec(substr($this->code_content_color, 1,2)), hexdec(substr($this->code_content_color, 3,2)), hexdec(substr($this->code_content_color, 5,2))));
//画布中插入像素点
$this->get_pix();
//画布中插入直线
$this->get_line();
//画布显示
header('Content-type:image/png');
imagepng($this->img);
}
//生成验证码
private function get_code(){
$str_content_len = strlen($this->str_content);
for($i=0;$i<4;$i++){
$this->code_content .= substr($this->str_content, mt_rand(0,$str_content_len-1),1);
}
}
//生成像素点
private function get_pix(){
for($j=0;$j<300;$j++){
$image_pix .= imagesetpixel($this->img, mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)));
}
return $image_pix;
}
//生成直线
private function get_line(){
for($l=0;$l<2;$l++){
$img_line .= imageline($this->img, mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)));
}
return $img_line;
}
//session存储验证码
private function session_code(){
session_start();
$_SESSION['code'] = $this->code_content;
}
//判断程序是否支持GD库
private function gdcheck(){
if(extension_loaded('gd')){
return true;
}else{
return false;
exit();
}
}
}
3、checkcode.php中的代码:
<?php
/**
* @Description 网站登录/注册验证码生成类
* @Author 赵一鸣
* @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
* @Date 2016年10月6日
*/
header('Content-type:text/html;charset="utf-8"');
session_start();
if($_POST['code']!=''){
if($_SESSION['code']==$_POST['code']){
echo '<script type="text/javascript">
alert("验证码填写成功");
history.go(-1);
</script>';
}else{
echo '<script type="text/javascript">
alert("验证码填写失败");
history.go(-1);
</script>';
}
}
4、showcode.php中的代码:
<?php
/**
* @Description 网站登录/注册验证码生成类
* @Author 赵一鸣
* @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
* @Date 2016年10月6日
*/
function __autoload($classname){
include strtolower($classname).'.class.php';
}
//定义验证码的取值范围
$str_content = 'abcdefghijklmnopqrstuvwxyz0123456789';
//验证码文字颜色
$code_content_color = '#ffffff';
//初始化对象
$code = new Createcode(100,30,$str_content,$code_content_color);
$code->get_img();
原文地址:http://www.zymseo.com/php/334.html
转载请注明出处!
PHP生成图片验证码demo【OOP面向对象版本】的更多相关文章
- python 全栈开发,Day85(Git补充,随机生成图片验证码)
昨日内容回顾 第一部分:django相关 1.django请求生命周期 1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端 请求头和请求体中会包含浏览器的动作(action),这 ...
- 用C实现OOP面向对象编程(1)
如摘要所说,C语言不支持OOP(面向对象的编程).并这不意味着我们就不能对C进行面向对象的开发,只是过程要复杂许多.原来以C++的许多工作,在C语言中需我们手动去完成. 博主将与大家一起研究一下如下用 ...
- OOP面向对象三大特点
OOP面向对象三大特点 (一)封装:将现实中一个事物的属性和功能集中定义在一个对象中.(创建对象) 创建对象的3种方式: 1.直接量方式:(创建一个单独的对象) var obj={ 属性名:值, ...
- 图片验证码demo示例
1.首先我们需要一个生成图片验证码图片的一个工具类(下方会有代码示例) 代码如下: package com.util; import java.awt.BasicStroke; import java ...
- net生成图片验证码--转自Lisliefor
目前,机器识别验证码已经相当强大了,比较常见的避免被机器识别的方法,就是将验证码的字符串连到一起,这样就加大的识别的难度,毕竟机器没有人工智能.我找了很多的.net生成图片验证码的例子,后来经过一些修 ...
- python PIL图像处理-生成图片验证码
生成效果如图: 代码 from PIL import Image,ImageDraw,ImageFont,ImageFilter import random # 打开一个jpg图像文件: im = I ...
- OOP 面向对象 七大原则 (二)
OOP 面向对象 七大原则 (二) 上一篇写到了前四个原则,这一篇继续~~ 接口隔离:客户端不应该依赖它不需要的接口:一个类对另一个类的依赖应该建立在最小的接口上. 又是一句大白话~就是说接口尽量 ...
- OOP 面向对象 七大原则 (一)
OOP 面向对象 七大原则 (一) 大家众所周知,面向对象有三大特征继承封装多态的同时,还具有这七大原则,三大特征上一篇已经详细说明,这一篇就为大家详解一下七大原则: 单一职责原则,开闭原则,里氏 ...
- OOP面向对象 三大特征 继承封装多态
OOP面向对象 ----三大特征 继承封装多态 面向对象(Object Oriented,OO)是软件开发方法.面向对象的概念和应用已超越了程序设计和软件开发,扩展到如数据库系统.交互式界面.应用结构 ...
随机推荐
- Windows Tomcat7.0 安装 Solr
准备工作 1.下载Tomcat7.0 ,apache-tomcat-7.0.67.exe,安装目录如下:C:\workspace\Tomcat7.0\ 2.下载Solr 5.2,solr-5.2.0. ...
- [AX 2012] Woker user request
在HR模块和System administrator模块下都能找到Woker user request这个功能,它的作用是为员工创建一个AX账号.比如我们创建一个这样的user request: 注意 ...
- JS实现移动端图片延迟加载
图片延迟加载常见的有,jquery.lazyload.js,原生JS实现的echo.js.但是都是必须给图片设置宽高. 因为项目是移动端,而且无法在加载前知道图片的宽高,所以,只好自己写了一个. 既然 ...
- php中并发读写文件冲突的解决方案(文件锁应用示例)
PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适 ...
- 关于 Visual Studio 调试 Global 的一点总结
在开发 MVC 的项目中遇到了些问题,想通过调戏查看找问题的原因,发现无法调试 Global 中的 Application_Start 方法,在网上找遍了也没有相应的解决办法,在经过了很多次尝试之后仍 ...
- MacBook 蓝牙无法搜索设备
背景 经常把MacBook合上盖子就塞进包里,用时打开盖子就继续操作,偶尔会出现刚刚还在用的罗技蓝牙鼠标,重新打开笔记本后就连接不上了,而且也无法搜索到周边的蓝牙设备. 解决方案 快捷键:Option ...
- VMware虚拟机无法识别U盘解决方案
1. 本机情况: Win7操作系统,VMware虚拟机,虚拟机版本:VMware 7.1,安装Ubuntu10.10,现要求在主机上插入U盘,在虚拟机中显示. 2. 遇到问题: U盘只在Win7主 ...
- crontab 定时调度
crontab -e */1 8-23 * * * /www/target/sh/myorder.sh & 0 1 * * * /www/target/php/sh/mymoney.sh &a ...
- TFS Build Silverlight项目的两个问题
1.The Silverlight 4 SDK is not installed. 打开对应的Build Definition,Process -> Advanced -> MSBuild ...
- 文件比对工具(Beyond Compare)
文件比对工具: 工具名称:Beyond Compare 版本号:v3.3.13 下载地址:http://i.cnblogs.com/Files.aspx 官网最新版本下载地址:http://www.s ...