下面是我今天下午用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面向对象版本】的更多相关文章

  1. python 全栈开发,Day85(Git补充,随机生成图片验证码)

    昨日内容回顾 第一部分:django相关 1.django请求生命周期 1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端 请求头和请求体中会包含浏览器的动作(action),这 ...

  2. 用C实现OOP面向对象编程(1)

    如摘要所说,C语言不支持OOP(面向对象的编程).并这不意味着我们就不能对C进行面向对象的开发,只是过程要复杂许多.原来以C++的许多工作,在C语言中需我们手动去完成. 博主将与大家一起研究一下如下用 ...

  3. OOP面向对象三大特点

    OOP面向对象三大特点 (一)封装:将现实中一个事物的属性和功能集中定义在一个对象中.(创建对象) 创建对象的3种方式: 1.直接量方式:(创建一个单独的对象) var obj={ 属性名:值,    ...

  4. 图片验证码demo示例

    1.首先我们需要一个生成图片验证码图片的一个工具类(下方会有代码示例) 代码如下: package com.util; import java.awt.BasicStroke; import java ...

  5. net生成图片验证码--转自Lisliefor

    目前,机器识别验证码已经相当强大了,比较常见的避免被机器识别的方法,就是将验证码的字符串连到一起,这样就加大的识别的难度,毕竟机器没有人工智能.我找了很多的.net生成图片验证码的例子,后来经过一些修 ...

  6. python PIL图像处理-生成图片验证码

    生成效果如图: 代码 from PIL import Image,ImageDraw,ImageFont,ImageFilter import random # 打开一个jpg图像文件: im = I ...

  7. OOP 面向对象 七大原则 (二)

    OOP 面向对象   七大原则 (二) 上一篇写到了前四个原则,这一篇继续~~ 接口隔离:客户端不应该依赖它不需要的接口:一个类对另一个类的依赖应该建立在最小的接口上. 又是一句大白话~就是说接口尽量 ...

  8. OOP 面向对象 七大原则 (一)

    OOP 面向对象   七大原则 (一) 大家众所周知,面向对象有三大特征继承封装多态的同时,还具有这七大原则,三大特征上一篇已经详细说明,这一篇就为大家详解一下七大原则: 单一职责原则,开闭原则,里氏 ...

  9. OOP面向对象 三大特征 继承封装多态

    OOP面向对象 ----三大特征 继承封装多态 面向对象(Object Oriented,OO)是软件开发方法.面向对象的概念和应用已超越了程序设计和软件开发,扩展到如数据库系统.交互式界面.应用结构 ...

随机推荐

  1. Window中调试HBase问题小结

    1.好久没用log4j了,转到logback好多年了,hbase程序运行时,报缺少log4j配置,那么,就转去logback吧(以下的XXX表示版本号). 原先lib包里面有log4j-XXX.jar ...

  2. 严重: Exception starting filter struts2解决方法!

    转自:http://blog.knowsky.com/260578.htm 问题出现: 严重: Exception starting filter struts2java.lang.ClassNotF ...

  3. HTTP 头部详细解释

    HTTP 头部解释 ================================================   Accept 告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,ty ...

  4. 解决Visual Studio 2010新建工程时出现『1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt』错误

    VS2010在经历一些更新后,建立Win32 Console Project时会出"error LNK1123" 错误.   解决方案为: 第一步:将:项目|项目属性|配置属性|清 ...

  5. [转]android开发之字节顺序

    原文在此 android上C++程序为小端字节顺序,和windows上一样. 而android上的JAVA程序则使用的是大端字节顺序. 用NDK和java SDK 做android程序时发现的问题,记 ...

  6. Selenium自动化测试项目案例实践公开课

    Selenium自动化测试项目案例实践公开课: http://gdtesting.cn/news.php?id=55

  7. Bulk Insert的用法 .

    /******* 导出到excel */EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:/temp1.xls -c -q -S&quo ...

  8. 动态密码卡TOTP算法

    TOTP NET实现:http://googleauthcsharp.codeplex.com/ 引用:http://www.cnblogs.com/wangxin201492/p/5030943.h ...

  9. NTKO Officecontrol在线Word编辑器的使用

    1.何时进行手工卸载和安装 一般情况下应该让客户端自动控件,这样当服务器控件版本更新时,客户端可以获得自动升级方面的好处.但是,如果因为客户机配置有问题,或者有其它拦截工具拦截的原因无法自动安装控件, ...

  10. 【转载】关于 Ubuntu 的小知识分享

    转载自:http://os.51cto.com/art/201307/402197.htm 一.默认开机直接进入到Ubuntu命令行界面 安装Ubuntu后,开机会默认进入到图形界面,如果不喜欢图形界 ...