-
php 中文汉字验证码生成程序
这款关于php 中文汉字验证码生成程序其实很简单的,生成中文汉字验证码需要字体来支持了,下面我们用的arial.ttf来支持生成,否则生成中文图片验证码会出乱码的.
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 (emptyempty($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 < 4; ++$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 < 4; ++$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 < 200; ++$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] < 200) $temp[2] = 255;
- $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 );
- }
- //开源代码phpfensi.com
- /**
- * 获取随机生成的验证码
- * @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 < 0) $h += 360;
- 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 (emptyempty( $_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 < 400; $px = $px + 1)
- {
- $y = $k * sin( 0.1 * ($px) ); //$y=200+10*sin(0.1*($px-200));
- for ($i = 0; $i < 2; $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://www.phpfensi.com/php/20140819/4468.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式