下面是我今天下午用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. Android 中ViewPagerIndicator的使用

    1.https://github.com/JakeWharton/Android-ViewPagerIndicator 2.http://blog.csdn.net/xiaanming/article ...

  2. HTML5 Canvas实战之刮奖效果

    近年来由于移动设备对HTML5的较好支持,经常有活动用刮奖的效果,最近也在看H5方面的内容,就自己实现了一个,现分享出来跟大家交流. 1.效果 2.原理 原理很简单,就是在刮奖区添加两个canvas, ...

  3. EDM博主笔记:EDM邮件营销的几个细节问题

    其实说起EDM邮件营销很多做过的人都知道,目前国内邮件营销的效果其实是比较差的,为什么?因为国内没有多少使用邮件的习惯,如果不是工作所需估计很多的人都几天不碰邮件了,但是反观国外 邮件是其日常的一部分 ...

  4. ping: sendto: Network is unreachable

    在我的板子上ping路由上的IP的时候可以ping通,但是ping外网的IP的时候提示"ping: sendto: Network is unreachable" 后来使用rout ...

  5. Core Animation 学习

    core animation 是在UIKit层之下的一个图形库,用于在iOS 和 OS X 实现动画. Core Animation管理App内容 core animation不是一个完整的绘图系统, ...

  6. java trim

    rim方法一般用来去除空格,但是根据JDK API的说明,该方法并不仅仅是去除空格,它能够去除从编码’\u0000′ 至 ‘\u0020′ 的所有字符. 回车换行也在这20个字符之中,以下是一个示例: ...

  7. JavaScript手札:《编写高质量JS代码的68个有效方法》(一)(1~5)

    编写高质量JS代码的68个有效方法(一) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...

  8. 左倾堆(一)之 图文解析 和 C语言的实现

    概要 本章介绍左倾堆,它和二叉堆一样,都是堆结构中的一员.和以往一样,本文会先对左倾堆的理论知识进行简单介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现:实现的语言虽不同,但是原理 ...

  9. linux主机间复制文件

    命令基本格式: 1.从 本地 复制到 远程    * 复制文件:          * 命令格式:                  scp local_file remote_username@re ...

  10. webpack多页面开发与懒加载hash解决方案

    之前讨论了webpack的hash与chunkhash的区别以及各自的应用场景,如果是常规单页面应用的话,上篇文章提供的方案是没有问题的.但是前端项目复杂多变,应对复杂多页面项目时,我们不得不继续踩w ...