直接附上代码吧!很简单的代码,写一遍基本就会了,主要明白用GD库画图的几个步骤:

1.先创建画布,不然画在哪儿?(imagecreatetruecolor)
2.根据要画的数据,选定颜色 (imagecolorallocate)
3.颜色有了,该考虑画啥了,画圆形,矩形,还是扇形,你喜欢就OK!
4.用什么画(系统中提供很多函数,比如imagerectangle,imagearc)
5.画完之后,有三个选择,最简单的,就是什么也不做;另外两个选择就是显示或保存,可以同时做(imagepng,imagegif)
6.图用完之后,就该释放资源了,有始有终嘛(imagedestroy)

代码如下:

<?php
/**
* 该类实例化的时候需要3个参数
* $width;//验证码的宽,默认值为80px
* $height;//验证码的高,默认值为20px
* $num;//验证码字符的个数,默认值为4
*/
class Code{
private $width;//验证码的宽
private $height;//验证码的高
private $num;//验证码字符的个数
private $code;//验证码的字符串
private $img;//验证码source
function __construct($width=80,$height=20,$num=4){
$this->width=$width;
$this->height=$height;
$this->num=$num;
$this->code=$this->create_code();
}
private function create_canvas(){//创建画布
$this->img=imagecreatetruecolor($this->width,$this->height);
$background_color=imagecolorallocate($this->img,0xFF,0xFF,0xFF);
imagefill($this->img,0,0,$background_color);
$border_color=imagecolorallocate($this->img,0xAA,0xAA,0xAA);
imagerectangle($this->img,0,0,$this->width-1,$this->height-1,$border_color);
}
private function create_code(){//生成验证码的字符串
$src="3456789qwertyupkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM";
$code="";
for($i=0;$i<$this->num;$i++){
$index=mt_rand(0,strlen($src)-1);
$code.=$src[$index];
}
return $code;
}
private function paint_char(){//将生成的字符串画在画布上
for($i=0;$i<$this->num;$i++){
$char_color=imagecolorallocate($this->img,0xFF,0,0xFF);
$font_size=4;
$x=5+($this->width/$this->num)*$i;
$y=($this->height-imagefontheight($font_size))/2; imagechar($this->img,$font_size,$x,$y,$this->code[$i],$char_color);
}
}
private function add_disturbance(){//添加干扰标记
for($i=0;$i<20;$i++){
$color=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($this->img,rand(1,$this->width-2),rand(1,$this->height-2),$color);
}
}
private function print_code() {//判断兼容哪种格式
if (imagetypes() & IMG_PNG) {
header("Content-type: image/png");
imagepng($this->img);
} elseif (imagetypes() & IMG_JPG) {
header("Content-type: image/jpeg");
imagejpeg($this->img);
} else {
die("No image support in this PHP server");
}
}
private function get_code(){//获取验证码字符串的值
return $this->code;
}
private function destroy_code(){//释放资源
imagedestroy($this->img);
}
function show_image_code(){//所有步骤的汇集,搞定所有验证码的工作
$this->create_canvas();
$this->paint_char();
$this->add_disturbance();
$this->print_code();
$this->destroy_code();
} } //测试
$code = new Code(80, 30, 4);
$code->show_image_code(); ?>

  

PHP常用类------生成验证码类Code的更多相关文章

  1. C#生成验证码类

    using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Drawing2D;us ...

  2. .net 根据匿名类生成实体类,根据datatable生成实体类,根据sql生成实体类

    在开发中可能会遇到这几种情况 1.EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类 2.通过datatable反射实体需要先建一个类 ,头痛 3.通过SQL语句返回的实体也需要 ...

  3. pdo文字水印类,验证码类,缩略图类,logo类

    文字水印类 image.class.php <?php /** * webrx.cn qq:7031633 * @author webrx * @copyright copyright (c) ...

  4. Common工具类的验证码类的使用(未实现验证)

    验证码接收 using System; using System.Collections.Generic; using System.Linq; using System.Web; using CZB ...

  5. Asp.net mvc生成验证码

    1.生成验证码类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  6. J2EE如何生成验证码图片和点击刷新验证码

    验证码图片生成步骤 创建BufferedImage对象. 获取BufferedImage的画笔,即调用getGraphics()方法获取Graphics对象. 调用Graphics对象的setColo ...

  7. Java生成验证码_转

    为了防止用户恶意,或者使用软件外挂提交一些内容,就得用验证码来阻止,虽然这个会影响用户体验,但为了避免一些问题很多网站都使用了验证码;今天下午参考文档弄了一个验证码,这里分享一下;这是一个web工程, ...

  8. Laravel 下生成验证码的类

    <?php namespace App\Tool\Validate; //验证码类 class ValidateCode { private $charset = 'abcdefghkmnprs ...

  9. JavaWeb学习总结(七):通过Servlet生成验证码及其应用 (BufferedImage类)

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

随机推荐

  1. python之面向对象进阶3

    1.isinstace和issubclass 2.staticmethod和classmethod 3.反射(hasattr.getattr.setattr.delattr等四个方法) 4.内置方法 ...

  2. css权重 vs 浏览器渲染 -- css之弊病

    昨日,突现一个bug,令人十分恼火. 基本场景 自己实现一多选日历,可多选多天(相连或不相连均可)."贵司"的需求真心有些小复杂了,"市面"上没有这样的相似的东 ...

  3. Arduino IDE for ESP8266 项目(3)创建AP+STA

    官网API:http://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html STA (客户端)手机连接路由器 S1 *简 ...

  4. 转载 线程池之ThreadPool类与辅助线程 - <第二篇>

    http://www.cnblogs.com/kissdodog/archive/2013/03/28/2986026.html 一.CLR线程池 管理线程开销最好的方式: 尽量少的创建线程并且能将线 ...

  5. 深入springboot原理——动手封装一个starter

    从上一篇文章<深入springboot原理——一步步分析springboot启动机制(starter机制)> 我们已经知道springboot的起步依赖与自动配置的机制.spring-bo ...

  6. oracle(sql)基础篇系列(五)——PLSQL、游标、存储过程、触发器

    PL/SQL PL/SQL 简介 每一种数据库都有这样的一种语言,PL/SQL 是在Oracle里面的一种编程语言,在Oracle内部使用的编程语言.我们知道SQL语言是没有分支和循环的,而PL语言是 ...

  7. GIT 远程仓库:添加远程库、从远程库克隆

    到目前为止,我们已经掌握了如何在Git仓库里对一个文件进行时光穿梭,你再也不用担心文件备份或者丢失的问题了. 可是有用过集中式版本控制系统SVN的童鞋会站出来说,这些功能在SVN里早就有了,没看出Gi ...

  8. HNOI2018做题笔记

    HNOI2018 寻宝游戏(位运算.基数排序) 看到位运算就要按位考虑.二进制下,\(\land 1\)与\(\lor 0\)没有意义的,\(\land 0\)强制这一位变为\(0\),\(\lor ...

  9. UOJ400/LOJ2553 CTSC2018 暴力写挂 边分治、虚树

    传送门--UOJ 传送门--LOJ 跟隔壁通道是一个类型的 要求的式子中有两个LCA,不是很方便,因为事实上在这种题目中LCA一般都是枚举的对象-- 第二棵树上的LCA显然是动不了的,因为没有其他的量 ...

  10. MySql 数据库移植记录

    在使用长文本时,SqlServer 在以下情况下工作正常 [Property("CContent", ColumnType = "StringClob", Le ...