下面是我今天下午用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. [AX2012]发送广播邮件

    AX 2012可以使用MAPI或者SMTP发送邮件,MAPI是客户端方法,需要outlook的协作,而SMTP则是服务器端方法,要求SMTP允许AOS服务器通过它中继.这里要讲的就是如何通过SMTP发 ...

  2. 百度地图API 关键字模糊搜索

    http://api.map.baidu.com/place/v2/search?q=广场&region=汕头&output=json&ak=5E56A48675a5cd09a ...

  3. WinStore控件之Button

    1 Buton入门简单应用 <StackPanel > <Button Content="按钮1" Height="80" Name=&quo ...

  4. android中的提示信息显示方法(toast应用)

    android中的提示信息显示方法(toast应用) (2011-10-17 11:02:06) 转载▼ 标签: android toast 杂谈 分类: Android android中toast的 ...

  5. ArcGIS Runtime for Android 使用异步GP服务绘制等值线

    关于基于Android上ArcGIS Server GP服务的调用,已经有前辈给出了很好的例子: http://blog.csdn.net/esrichinacd/article/details/92 ...

  6. Backbone1.0.0数据验证的变化

    0.5.3版本对Model数据验证时,绑定Error就可以了: (function(){ var Model = Backbone.Model.extend({ initialize : functi ...

  7. BATCH(BAT批处理命令语法)

    bat语法备忘扩展名是bat(在nt/2000/xp/2003下也可以是cmd)的文件就是批处理文件[@more@] bat语法备忘扩展名是bat(在nt/2000/xp/2003下也可以是cmd)的 ...

  8. 由于无法创建应用程序域,因此未能执行请求。错误: 0x80070002 系统找不到指定的文件

    前两天安装了一堆补丁后突然发现,原本正常使用了一年的应用系统打不开了,到事件日志中发现有.net framewok 2.0的补丁安装失败的日志,于从从重装补丁开始.到重新注册.net框架,再到所有.n ...

  9. 2012年中国科学技术信息研究所发布自然科学SCI(E)论文期刊分区列表

    分区 期刊名称 ISSN 学科名称 学科名称(中文) 总被引频次 影响因子 5年影响因子 即年影响因子 被引半衰期 特征因子 论文影响指标 1区 ACM COMPUT SURV 0360-0300 C ...

  10. There is an internal error in the React performance measurement code.Did not expect componentDidMount timer to start while render timer is still in progress for another instance

    一.There is an internal error in the React performance measurement code.Did not expect componentDidMo ...