php生成验证码类代码
php教程生成验证码类代码
*/
class securecode
{
private static $instance=null;
private $code = '';
private $fontfile;
private $validate;
private $image;
private $specialadd = 'special string for securecode';
private $codeexpire=86400;
private $codecookiename='secure_code';
/**
* 构造方法
*/
private function securecode()
{
$this->fontfile = dirname( __file__ ) . '/arial.ttf';
}
private function __construct()
{
$this->securecode();
}
public static function getinstance()
{
if (self::$instance==null)
self::$instance=new self();
return self::$instance;
}
/**
* 指定字体文件所在路径,默认为当前文件夹下arial.ttf文件
* @param $fontfile 文件路径
* @return void
*/
function loadfont($fontfile)
{
$this->fontfile = $fontfile;
}
/**
* 图片输出方法,在执行本方法前程序不应该有任何形式的输出
* @return void;
*/
function stroke()
{
$this->savecode();
self::sendheader();
imagegif( $this->validate );
imagedestroy( $this->validate );
imagedestroy( $this->image );
}
/**
* 图片保存方法
* @param $filename 保存路径
* @return void
*/
function save($filename)
{
$this->savecode();
imagegif( $this->validate , $filename );
imagedestroy( $this->validate );
imagedestroy( $this->image );
}
/**
* 验证码验证方法
* @param $input 要验证的字符串,即用户的输入内容
* @return boolean 验证结果
*/
function verify($input)
{
$input=strtolower($input);
$targetcode=$this->authcode($input);
$code=$this->getcookie();
if (empty($code)||$code!=$targetcode)
$result= false;
else
$result=true;
$_cookie[$this->codecookiename]='';
setcookie ( $this->codecookiename, '', - 1 );
return $result;
}
/**
* 图片创建方法
* @return void;
*/
function createimage()
{
$this->randcode();
$size = 30;
$width = 90;
$height = 35;
$degrees = array (
rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 )
);
for ($i = 0; $i
{
if (rand() % 2);
else $degrees[$i] = -$degrees[$i];
}
$this->image = imagecreatetruecolor( $size , $size );
$this->validate = imagecreatetruecolor( $width , $height );
$back = imagecolorallocate( $this->image , 255 , 255 , 255 );
$border = imagecolorallocate( $this->image , 0 , 0 , 0 );
imagefilledrectangle( $this->validate , 0 , 0 , $width , $height , $back );
for ($i = 0; $i
{
$temp = self::rgbtohsv( rand( 0 , 250 ) , rand( 0 , 150 ) , rand( 0 , 250 ) );
if ($temp[2] > 60) $temp[2] = 60;
$temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
$textcolor[$i] = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
}
for ($i = 0; $i
{
$randpixelcolor = imagecolorallocate( $this->validate , rand( 0 , 255 ) , rand( 0 , 255 ) , rand( 0 , 255 ) );
imagesetpixel( $this->validate , rand( 1 , 87 ) , rand( 1 , 35 ) , $randpixelcolor );
}
$temp = self::rgbtohsv( rand( 220 , 255 ) , rand( 220 , 255 ) , rand( 220 , 255 ) );
if ($temp[2]
$temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
$randlinecolor = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
self::imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] );
imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
putenv( 'gdfontpath=' . realpath( '.' ) );
// name the font to be used (note the lack of the .ttf extension
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[0] , $this->fontfile , $this->code[0] );
$this->image = imagerotate( $this->image , $degrees[0] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 1 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[1] , $this->fontfile , $this->code[1] );
$this->image = imagerotate( $this->image , $degrees[1] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 21 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[2] , $this->fontfile , $this->code[2] );
$this->image = imagerotate( $this->image , $degrees[2] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 41 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
$this->image = imagecreatetruecolor( $size , $size );
imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[3] , $this->fontfile , $this->code[3] );
$this->image = imagerotate( $this->image , $degrees[3] , $back );
imagecolortransparent( $this->image , $back );
imagecopymerge( $this->validate , $this->image , 61 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border );
}
/**
* 获取随机生成的验证码
* @return string 随机验证码,返回的验证码不进行任何处理
*/
function getcode()
{
return $this->code;
}
/**
* 生成随机码方法
* @return void;
*/
protected function randcode()
{
$alphastr = 'abcdefghijklmnpqrstuvwxyz123456789';
$randstr = array (
$alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}
);
$this->code = strtolower( $randstr[0] . $randstr[1] . $randstr[2] . $randstr[3] );
}
/**
* rgb色到hsv色转变方法
* @param $r
* @param $g
* @param $b
* @return array hsv数组
*/
protected static function rgbtohsv($r, $g, $b)
{
$tmp = min( $r , $g );
$min = min( $tmp , $b );
$tmp = max( $r , $g );
$max = max( $tmp , $b );
$v = $max;
$delta = $max - $min;
if ($max != 0) $s = $delta / $max; // s
else
{
$s = 0;
//$h = undefinedcolor;
return;
}
if ($r == $max) $h = ($g - $b) / $delta; // between yellow & magenta
else if ($g == $max) $h = 2 + ($b - $r) / $delta; // between cyan & yellow
else $h = 4 + ($r - $g) / $delta; // between magenta & cyan
$h *= 60; // degrees
if ($h
return array (
$h, $s, $v
);
}
/**
* 同上一方法功能相反
* @param $h
* @param $s
* @param $v
* @return array rgb数组
*/
protected static function hsvtorgb($h, $s, $v)
{
if ($s == 0)
{
// achromatic (grey)
$r = $g = $b = $v;
return;
}
$h /= 60; // sector 0 to 5
$i = floor( $h );
$f = $h - $i; // factorial part of h
$p = $v * (1 - $s);
$q = $v * (1 - $s * $f);
$t = $v * (1 - $s * (1 - $f));
switch ($i)
{
case 0 :
$r = $v;
$g = $t;
$b = $p;
break;
case 1 :
$r = $q;
$g = $v;
$b = $p;
break;
case 2 :
$r = $p;
$g = $v;
$b = $t;
break;
case 3 :
$r = $p;
$g = $q;
$b = $v;
break;
case 4 :
$r = $t;
$g = $p;
$b = $v;
break;
default : // case 5:
$r = $v;
$g = $p;
$b = $q;
break;
}
return array (
$r, $g, $b
);
}
/**
* 使用cookie保存验证码的方法
* @return void
*/
protected function savecode()
{
$code = $this->authcode($this->code);
$this->setcookie($code);
}
/**
* 验证码cookie值获取方法
* @return string cookie值
*/
protected function getcookie()
{
if (empty( $_cookie[$this->codecookiename] ))
{
return '';
}
else
{
return addslashes($_cookie[$this->codecookiename]);
}
}
/**
* 验证码cookie创建方法
* @param string $code 要保存的验证码
* @return void
*/
protected function setcookie($code)
{
$expire = $this->codeexpire > 0 ? $this->codeexpire + time() : 0;
setcookie( $this->codecookiename , $code, $expire );
}
/**
* 验证码加密方法
* @param string $code 要加密的随机码
* @return mixed 执行结果
*/
protected function authcode($code)
{
return md5($code.$this->specialadd);
}
/**
* 干扰线生成方法
* @param resource $image 图片资源句柄
* @param string $color 干扰线颜色
*/
protected static function imagelinethick($image, $color)
{
$k = rand( 5 , 20 );
for ($px = 0; $px
{
$y = $k * sin( 0.1 * ($px) ); //$y=200+10*sin(0.1*($px-200));
for ($i = 0; $i
{
imagesetpixel( $image , $px , $y + 10 + $i , $color );
}
}
}
/**
* http标头设置方法
* @return void
*/
protected static function sendheader()
{
header( "pragma: no-cache" );
header( "cache-control: max-age=1, s-maxage=1, no-cache, must-revalidate" );
header( 'content-type: image/gif' );
}
}
http://down.bKjia.c0m/down/code/php/qitayuanma/2010/1220/22330.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
