<?php
/**
* 图片验证码类
* 生成图片类型验证码,验证码包含数字和大写字母,session中存放md5加密后的验证码
*
* 使用方法:
* $captcha = new Catpcha();
* $captcha->buildAndExportImage();
*
* 作 者: luojing
* 创建时间: 2013-3-27 上午11:42:12
*/
class Captcha {
private $width;//宽度
private $height; //高度
private $codeNum;//验证码字符数量
private $image;//验证码图像资源
private $sessionKey;//session中保存的名字
private $captcha;//验证码字符串
const charWidth = 10;//单个字符宽度,根据输出字符大小而变
/**
* 创建验证码类,初始化相关参数
* @param $width 图片宽度
* @param $height 图片高度
* @param $codeNum 验证码字符数量
* @param $sessionKey session中保存的名字
*/
function __construct($width = 50, $height = 20, $codeNum = 4, $sessionKey = 'captcha') {
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->sessionKey = $sessionKey;
//保证最小高度和宽度
if($height < 20) {
$this->height = 20;
}
if($width < ($codeNum * self::charWidth + 10)) {//左右各保留5像素空隙
$this->width = $codeNum * self::charWidth + 10;
}
}
/**
* 构造并输出验证码图片
*/
public function buildAndExportImage() {
$this->createImage();
$this->setDisturb();
$this->setCaptcha();
$this->exportImage();
}
/**
* 构造图像,设置底色
*/
private function createImage() {
//创建图像
$this->image = imagecreatetruecolor($this->width, $this->height);
//创建背景色
$bg = imagecolorallocate($this->image, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
//填充背景色
imagefilledrectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $bg);
}
/**
* 设置干扰元素
*/
private function setDisturb() {
//设置干扰点
for($i = 0; $i < 150; $i++) {
$color = imagecolorallocate($this->image, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200));
imagesetpixel($this->image, mt_rand(5, $this->width - 10), mt_rand(5, $this->height - 3), $color);
}
//设置干扰线
for($i = 0; $i < 10; $i++) {
$color = imagecolorallocate($this->image, mt_rand(150, 220), mt_rand(150, 220), mt_rand(150, 220));
imagearc($this->image, mt_rand(-10, $this->width), mt_rand(-10, $this->height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $color);
}
//创建边框色
$border = imagecolorallocate($this->image, mt_rand(0, 50), mt_rand(0, 50), mt_rand(0, 50));
//画边框
imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $border);
}
/**
* 产生并绘制验证码
*/
private function setCaptcha() {
$str = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
//生成验证码字符
for($i = 0; $i < $this->codeNum; $i++) {
$this->captcha .= $str{mt_rand(0, strlen($str) - 1)};
}
//绘制验证码
for($i = 0; $i < strlen($this->captcha); $i++) {
$color = imagecolorallocate($this->image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200));
$x = floor(($this->width - 10)/$this->codeNum);
$x = $x*$i + floor(($x-self::charWidth)/2) + 5;
$y = mt_rand(2, $this->height - 20);
imagechar($this->image, 5, $x, $y, $this->captcha{$i}, $color);
}
}
/*
* 输出图像,验证码保存到session中
*/
private function exportImage() {
if(imagetypes() & IMG_GIF){
header('Content-type:image/gif');
imagegif($this->image);
} else if(imagetypes() & IMG_PNG){
header('Content-type:image/png');
imagepng($this->iamge);
} else if(imagetypes() & IMG_JPEG) {
header('Content-type:image/jpeg');
imagepng($this->iamge);
} else {
imagedestroy($this->image);
die("Don't support image type!");
}
//将验证码信息保存到session中,md5加密
if(!isset($_SESSION)){
session_start();
}
$_SESSION[$this->sessionKey] = md5($this->captcha);
imagedestroy($this->image);
}
function __destruct() {
unset($this->width, $this->height, $this->codeNum,$this->captcha);
}
}
分享到:
相关推荐
php 图片处理 验证码类,设置干扰像素,向图像中输出不同颜色的100个点,随机颜色、随机摆放、随机字符串向图像中输出
PHP 图片验证码.rar PHP 图片验证码.rar PHP 图片验证码.rar
PHP图片验证码的实现
php图片验证码,验证,点击图片刷新验证码
实用的PHP图片验证码,有干扰线,噪点,图片刷新的常用功能. 有些非ie浏览器上出现乱码,请添加 header('Content-type:image/jpeg');
php验证码类,可以用再其他类库上,删除在验证码图片输出前所有信息,保证验证码能正确生成。
php图片上传类,php验证码类,php分页类
一个PHP生成验证码图片以及进行验证的类 实例如下: 调用实例如下 require 'SecureCode.class.php'; $code=SecureCode::getInstance(); $code->createImage(); $code->stroke(); /* 如果需要保存验证码图片,...
很全的php验证码,包含语音和图片两种方式,可用于登录和投票验证。
php+js图片验证码
validatecodeimage 图片验证码类 php
验证码合集,验证码 PHP 语音 图片 多版本
定制化的php验证码类 创建验证码图像: 建立画布(__construct函数) 设置画布背景($this->set_bgcolor();) 获取随机字符串($this->get_randnum ();) 文字写到图片上(imagestring函数)...
PHP快速生成图片验证码并且实现验证插件 1.插件作用: 本插件可以快速实现网站验证码功能,包括验证码的生成和验证。 2.所需参数: CaptchaTool类包括两个方法,generate方法可以实现生成'...
PHP生成GIF动态图片验证码
PHP实现简单实用的验证码类,$image->config('宽度','高度','字符个数','验证码session索引'),$image->create();//这样就会向浏览器输出一张图片,产生验证码,产生验证码图片,设置图片的干扰像素,在验证码图片上...
使用PHP生成扭曲字符的图片验证码,可控制扭曲程度(调整扭曲度和倾斜角度),自定义图片大小,调用简单。
讲述了PHP实现简单实用的验证码类,$image->config('宽度','高度','字符个数','验证码session索引'),$image->create();//这样就会向浏览器输出一张图片,产生验证码,产生验证码图片,设置图片的干扰像素,在验证码...
只要在创建对象时,为构造方法提供三个参数,包括创建验证码图片的宽度、高度及验证码字母个数,就可以成功创建一个验证码类的对象。在脚本code.php中,使用session_start()开启用户会话控制,然后包含验证码类Vcode...