<?php
/**
* ECMALL: 验证码类
* ============================================================================
* 版权所有 (C) 2005-2008 上海商派网络科技有限公司,并保留所有权利。
* 网站地址: http://www.shopex.cn
* -------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用;
* 不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* $Id: captcha.lib.php 7840 2009-05-21 06:14:05Z lizhaosheng $
*/
if (!defined('IN_TCZ'))
{
trigger_error('Hacking attempt', E_USER_ERROR);
}
/**
* 用例如下
完整写法
include_once(ROOT_PATH.'/includes/cls.captcha.new.php');
$seccode = 'asdf';
$code = new Captcha();
$code->code = $seccode;
$code->width = 150;
$code->height = 60;
$code->background = 1;
$code->adulterate = 1;
$code->ttf = 1;
$code->angle = 1;
$code->color = 1;
$code->size = 1;
$code->shadow = 1;
$code->animator = 0;
$code->display();
$code->fontpath = ROOT_PATH.'/includes/captcha/fonts/';
$code->imagepath = ROOT_PATH.'/includes/captcha/';
简单用法
include_once(ROOT_PATH.'/includes/cls.captcha.new.php');
$seccode = 'asdf';
$code = new Captcha();
$code->code = $seccode;
$code->display();
*/
class Captcha
{
var $code; //a-z 范围内随机
var $width = 150; //宽度
var $height = 60; //高度
var $background = 1; //随机图片背景
var $adulterate = 1; //随机背景图形
var $ttf = 1; //随机 TTF 字体
var $angle = 0; //随机倾斜度
var $color = 1; //随机颜色
var $size = 0; //随机大小
var $shadow = 1; //文字阴影
var $animator = 0; //GIF 动画
var $fontpath = ''; //TTF字库目录
var $imagepath = ''; //图片目录
var $fontcolor;
var $im;
function __construct($options = array())
{
$this->Captcha($options);
}
function Captcha($options = array())
{
$this->fontpath = ROOT_PATH . '/includes/captcha/fonts/';
$this->imagepath = ROOT_PATH . '/includes/captcha/';
isset($options['width']) && $this->width = $options['width'];
isset($options['height'])&& $this->height= $options['height'];
}
function display($code)
{
session_cache_limiter('nocache');
$this->code = strtoupper($code);
if (function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') && function_exists('imagecolorallocate') && function_exists('imagechar') && function_exists('imagecolorsforindex') && function_exists('imageline') && function_exists('imagecreatefromstring') && (function_exists('imagegif') || function_exists('imagepng') || function_exists('imagejpeg')))
{
$this->image();
}
else
{
$this->bitmap();
}
}
function image()
{
$bgcontent = $this->background();
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
if ($this->animator == 1 && function_exists('imagegif'))
{
import('gifmerge.lib');
$trueframe = mt_rand(1, 9);
for ($i = 0; $i <= 9; $i++)
{
$this->im = imagecreatefromstring($bgcontent);
$x[$i] = $y[$i] = 0;
$this->adulterate && $this->adulterate();
if ($i == $trueframe)
{
$this->ttf && function_exists('imagettftext') ? $this->ttffont() : $this->giffont();
$d[$i] = mt_rand(250, 400);
}
else
{
$this->adulteratefont();
$d[$i] = mt_rand(5, 15);
}
ob_start();
imagegif($this->im);
imagedestroy($this->im);
$frame[$i] = ob_get_contents();
ob_end_clean();
}
$anim = new GifMerge($frame, 255, 255, 255, 0, $d, $x, $y, 'C_MEMORY');
header('Content-type: image/gif');
echo $anim->getAnimation();
}
else
{
$this->im = imagecreatefromstring($bgcontent);
$this->adulterate && $this->adulterate();
$this->ttf && function_exists('imagettftext') ? $this->ttffont() : $this->giffont();
if (function_exists('imagepng'))
{
header('Content-type: image/png');
imagepng($this->im);
}
else
{
header('Content-type: image/jpeg');
imagejpeg($this->im, '', 100);
}
imagedestroy($this->im);
}
}
function background() {
$this->im = imagecreatetruecolor($this->width, $this->height);
$backgroundcolor = imagecolorallocate($this->im, 255, 255, 255);
$backgrounds = $c = array();
if ($this->background && function_exists('imagecreatefromjpeg') && function_exists('imagecolorat') && function_exists('imagecopymerge') && function_exists('imagesetpixel') && function_exists('imageSX') && function_exists('imageSY'))
{
if ($handle = @opendir($this->imagepath.'background/'))
{
while ($bgfile = @readdir($handle))
{
if (preg_match('/\.jpg$/i', $bgfile))
{
$backgrounds[] = $this->imagepath.'background/'.$bgfile;
}
}
@closedir($handle);
}
if ($backgrounds)
{
$imwm = imagecreatefromjpeg($backgrounds[array_rand($backgrounds)]);
$colorindex = imagecolorat($imwm, 0, 0);
$this->c = imagecolorsforindex($imwm, $colorindex);
$colorindex = imagecolorat($imwm, 1, 0);
imagesetpixel($imwm, 0, 0, $colorindex);
$c[0] = $c['red'];$c[1] = $c['green'];$c[2] = $c['blue'];
imagecopymerge($this->im, $imwm, 0, 0, mt_rand(0, 200 - $this->width), mt_rand(0, 80 - $this->height), imageSX($imwm), imageSY($imwm), 100);
imagedestroy($imwm);
}
}
if (!$this->background || !$backgrounds)
{
for ($i = 0; $i < 3; $i++)
{
$start[$i] = mt_rand(200, 255);$end[$i] = mt_rand(100, 150);$step[$i] = ($end[$i] - $start[$i]) / $this->width;$c[$i] = $start[$i];
}
for ($i = 0; $i < $this->width; $i++)
{
$color = imagecolorallocate($this->im, $c[0], $c[1], $c[2]);
imageline($this->im, $i, 0, $i-$angle, $this->height, $color);
$c[0] += $step[0];$c[1] += $step[1];$c[2] += $step[2];
}
$c[0] -= 20;$c[1] -= 20;$c[2] -= 20;
}
ob_start();
if (function_exists('imagepng'))
{
imagepng($this->im);
}
else
{
imagejpeg($this->im, '', 100);
}
imagedestroy($this->im);
$bgcontent = ob_get_contents();
ob_end_clean();
$this->fontcolor = $c;
return $bgcontent;
}
function adulterate()
{
$linenums = $this->height / 10;
for ($i=0; $i <= $linenums; $i++)
{
$color = $this->color ? imagecolorallocate($this->im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)) : imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
$x = mt_rand(0, $this->width);
$y = mt_rand(0, $this->height);
if (mt_rand(0, 1))
{
imagearc($this->im, $x, $y, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, 360), mt_rand(0, 360), $color);
}
else
{
imageline($this->im, $x, $y, $linex + mt_rand(0, $linemaxlong), $liney + mt_rand(0, mt_rand($this->height, $this->width)), $color);
}
}
}
function adulteratefont()
{
$seccodeunits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$x = $this->width / 4;
$y = $this->height / 10;
$text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
for ($i = 0; $i <= 3; $i++)
{
$adulteratecode = $seccodeunits{mt_rand(0, 26)};
imagechar($this->im, 5, $x * $i + mt_rand(0, $x - 10), mt_rand($y, $this->height - 10 - $y), $adulteratecode, $text_color);
}
}
function ttffont()
{
$seccode = $this->code;
$charset = $GLOBALS['charset'];
$seccoderoot = $this->fontpath;
$dirs = opendir($seccoderoot);
$seccodettf = array();
while ($entry = readdir($dirs))
{
if ($entry != '.' && $entry != '..' && in_array(strtolower(file_ext($entry)), array('ttf', 'ttc')))
{
$seccodettf[] = $entry;
}
}
$seccodelength = 4;
$widthtotal = 0;
for ($i = 0; $i < $seccodelength; $i++)
{
$font[$i]['font'] = $seccoderoot.$seccodettf[array_rand($seccodettf)];
$font[$i]['angle'] = $this->angle ? mt_rand(-30, 30) : 0;
$font[$i]['size'] = $this->width / 4;
$this->size && $font[$i]['size'] = mt_rand($font[$i]['size'] - $this->width / 40, $font[$i]['size'] + $this->width / 20);
$box = imagettfbbox($font[$i]['size'], 0, $font[$i]['font'], $seccode[$i]);
$font[$i]['zheight'] = max($box[1], $box[3]) - min($box[5], $box[7]);
$box = imagettfbbox($font[$i]['size'], $font[$i]['angle'], $font[$i]['font'], $seccode[$i]);
$font[$i]['height'] = max($box[1], $box[3]) - min($box[5], $box[7]);
$font[$i]['hd'] = $font[$i]['height'] - $font[$i]['zheight'];
$font[$i]['width'] = (max($box[2], $box[4]) - min($box[0], $box[6])) + mt_rand(0, $this->width / 8);
$font[$i]['width'] = $font[$i]['width'] > $this->width / $seccodelength ? $this->width / $seccodelength : $font[$i]['width'];
$widthtotal += $font[$i]['width'];
}
$x = mt_rand($font[0]['angle'] > 0 ? cos(deg2rad(90 - $font[0]['angle'])) * $font[0]['zheight'] : 1, $this->width - $widthtotal);
!$this->color && $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
for ($i = 0; $i < $seccodelength; $i++)
{
if ($this->color)
{
$this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
$this->shadow && $text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
$text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
}
elseif ($this->shadow)
{
$text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
}
$y = $font[0]['angle'] > 0 ? mt_rand($font[$i]['height'], $this->height) : mt_rand($font[$i]['height'] - $font[$i]['hd'], $this->height - $font[$i]['hd']);
$this->shadow && imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x + 1, $y + 1, $text_shadowcolor, $font[$i]['font'], $seccode[$i]);
imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x, $y, $text_color, $font[$i]['font'], $seccode[$i]);
$x += $font[$i]['width'];
}
}
function giffont()
{
$seccode = $this->code;
$seccodedir = array();
if (function_exists('imagecreatefromgif'))
{
$seccoderoot = $this->imagepath.'gif/';
$dirs = opendir($seccoderoot);
while ($dir = readdir($dirs))
{
if ($dir != '.' && $dir != '..' && is_file($seccoderoot.$dir.'/9.gif'))
{
$seccodedir[] = $dir;
}
}
}
$widthtotal = 0;
for ($i = 0; $i <= 3; $i++)
{
$this->imcodefile = $seccodedir ? $seccoderoot.$seccodedir[array_rand($seccodedir)].'/'.strtolower($seccode[$i]).'.gif' : '';
if (!empty($this->imcodefile) && is_file($this->imcodefile))
{
$font[$i]['file'] = $this->imcodefile;
$font[$i]['data'] = getimagesize($this->imcodefile);
$font[$i]['width'] = $font[$i]['data'][0] + mt_rand(0, 6) - 4;
$font[$i]['height'] = $font[$i]['data'][1] + mt_rand(0, 6) - 4;
$font[$i]['width'] += mt_rand(0, $this->width / 5 - $font[$i]['width']);
$widthtotal += $font[$i]['width'];
}
else
{
$font[$i]['file'] = '';
$font[$i]['width'] = 8 + mt_rand(0, $this->width / 5 - 5);
$widthtotal += $font[$i]['width'];
}
}
$x = mt_rand(1, $this->width - $widthtotal);
for ($i = 0; $i <= 3; $i++)
{
$this->color && $this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
if ($font[$i]['file'])
{
$this->imcode = imagecreatefromgif($font[$i]['file']);
if ($this->size)
{
$font[$i]['width'] = mt_rand($font[$i]['width'] - $this->width / 20, $font[$i]['width'] + $this->width / 20);
$font[$i]['height'] = mt_rand($font[$i]['height'] - $this->width / 20, $font[$i]['height'] + $this->width / 20);
}
$y = mt_rand(0, $this->height - $font[$i]['height']);
if ($this->shadow)
{
$this->imcodeshadow = $this->imcode;
imagecolorset($this->imcodeshadow, 0 , 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
imagecopyresized($this->im, $this->imcodeshadow, $x + 1, $y + 1, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]);
}
imagecolorset($this->imcode, 0 , $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
imagecopyresized($this->im, $this->imcode, $x, $y, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]);
}
else
{
$y = mt_rand(0, $this->height - 20);
if ($this->shadow)
{
$text_shadowcolor = imagecolorallocate($this->im, 255 - $this->fontcolor[0], 255 - $this->fontcolor[1], 255 - $this->fontcolor[2]);
imagechar($this->im, 5, $x + 1, $y + 1, $seccode[$i], $text_shadowcolor);
}
$text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
imagechar($this->im, 5, $x, $y, $seccode[$i], $text_color);
}
$x += $font[$i]['width'];
}
}
function bitmap()
{
$numbers = array
(
'2' => array('fc','c0','60','30','18','0c','cc','cc','78','00'),
'3' => array('78','8c','0c','0c','38','0c','0c','8c','78','00'),
'4' => array('00','3e','0c','fe','4c','6c','2c','3c','1c','1c'),
'5' => array('78','8c','0c','0c','4c','78','60','60','7c','00'),
'7' => array('30','30','38','18','18','18','1c','8c','fc','00'),
'A' => array('00','EE','6c','7c','6c','6c','28','38','f8','00'),
'C' => array('00','38','64','c0','c0','c0','c4','64','3c','00'),
'E' => array('00','fe','62','62','68','78','6a','62','fe','00'),
'F' => array('00','f8','60','60','68','78','6a','62','fe','00'),
'H' => array('00','e7','66','66','66','7e','66','66','e7','00'),
'K' => array('00','f3','66','66','7c','78','6c','66','f7','00'),
'M' => array('00','f7','63','6b','6b','77','77','77','e3','00'),
'N' => array('00','ec','4c','54','54','54','64','64','ee','00'),
'P' => array('00','f8','60','60','7c','66','66','66','fc','00'),
'R' => array('00','f3','66','6c','7c','66','66','66','fc','00'),
'T' => array('00','78','30','30','30','30','b4','b4','fc','00'),
'V' => array('00','1c','1c','36','36','36','63','63','f7','00'),
'W' => array('00','36','36','36','77','7f','6b','63','f7','00'),
'X' => array('00','f7','66','3c','18','18','3c','66','ef','00'),
'Y' => array('00','7e','18','18','18','3c','24','66','ef','00'),
);
foreach ($numbers as $i => $number)
{
for ($j = 0; $j < 6; $j++)
{
$a1 = substr('012', mt_rand(0, 2), 1).substr('012345', mt_rand(0, 5), 1);
$a2 = substr('012345', mt_rand(0, 5), 1).substr('0123', mt_rand(0, 3), 1);
mt_rand(0, 1) == 1 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a1);
mt_rand(0, 1) == 0 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a2);
}
}
$bitmap = array();
for ($i = 0; $i < 20; $i++)
{
for ($j = 0; $j <= 3; $j++)
{
$bytes = $numbers[$this->code[$j]][$i];
$a = mt_rand(0, 14);
array_push($bitmap, $bytes);
}
}
for ($i = 0; $i < 8; $i++)
{
$a = substr('012345', mt_rand(0, 2), 1) . substr('012345', mt_rand(0, 5), 1);
array_unshift($bitmap, $a);
array_push($bitmap, $a);
}
$image = pack('H*', '424d9e000000000000003e000000280000002000000018000000010001000000'.
'0000600000000000000000000000000000000000000000000000FFFFFF00'.implode('', $bitmap));
header('Content-Type: image/bmp');
echo $image;
}
}
/**
* 以下内容取自thinkphp,输出中文验证码
* 若新加方法,请勿写在此范围内
**/
class Chinesecaptcha
{
// 中文验证码
function GBVerify($length=4, $type='png', $width=180, $height=50 ,$code='',$answer='', $fontface='simhei.ttf', $verifyName='chineseverify') {
if(!$code){
$code = $this->rand_string($length, 4);
}else{
preg_match_all("/([\x{4e00}-\x{9fa5}“”?]){1}/u",$code,$arrCh);
$length = strlen($code) - count($arrCh[0])*2;
}
$width = ($length * 45) > $width ? $length * 45 : $width;
$_SESSION[$verifyName] = $answer ? md5(strtolower(trim($answer))) : md5(strtolower(trim($code)));
$im = imagecreatetruecolor($width, $height);
$borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
$bkcolor = imagecolorallocate($im, 250, 250, 250);
imagefill($im, 0, 0, $bkcolor);
@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
// 干扰
for ($i = 0; $i < 3; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
}
// for ($i = 0; $i < 255; $i++) {
// $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
// imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $fontcolor);
// }
if (!is_file($fontface)) {
$fontface = dirname(__FILE__) . "/" . $fontface;
}
for ($i = 0; $i < $length; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); //这样保证随机出来的颜色较深。
$codex = $this->msubstr($code, $i, 1);
imagettftext($im, mt_rand(16, 20), mt_rand(0, 10), 40 * $i + 20, mt_rand(30, 35), $fontcolor, $fontface, $codex);
}
$this->output($im, $type);
}
/**
* 产生随机字串,可用来自动生成密码
* 默认长度6位 字母和数字混合 支持中文
* @param string $len 长度
* @param string $type 字串类型
* 0 字母 1 数字 其它 混合
* @param string $addChars 额外字符
* @return string
*/
function rand_string($len=6,$type='',$addChars='') {
$str ='';
switch($type) {
case 0:
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.$addChars;
break;
case 1:
$chars= str_repeat('0123456789',3);
break;
case 2:
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addChars;
break;
case 3:
$chars='abcdefghijklmnopqrstuvwxyz'.$addChars;
break;
case 4:
$chars = "们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书术状厂须离再目海交权且儿青才证低越际八试规斯近注办布门铁需走议县兵固除般引齿千胜细影济白格效置推空配刀叶率述今选养德话查差半敌始片施响收华觉备名红续均药标记难存测士身紧液派准斤角降维板许破述技消底床田势端感往神便贺村构照容非搞亚磨族火段算适讲按值美态黄易彪服早班麦削信排台声该击素张密害侯草何树肥继右属市严径螺检左页抗苏显苦英快称坏移约巴材省黑武培著河帝仅针怎植京助升王眼她抓含苗副杂普谈围食射源例致酸旧却充足短划剂宣环落首尺波承粉践府鱼随考刻靠够满夫失包住促枝局菌杆周护岩师举曲春元超负砂封换太模贫减阳扬江析亩木言球朝医校古呢稻宋听唯输滑站另卫字鼓刚写刘微略范供阿块某功套友限项余倒卷创律雨让骨远帮初皮播优占死毒圈伟季训控激找叫云互跟裂粮粒母练塞钢顶策双留误础吸阻故寸盾晚丝女散焊功株亲院冷彻弹错散商视艺灭版烈零室轻血倍缺厘泵察绝富城冲喷壤简否柱李望盘磁雄似困巩益洲脱投送奴侧润盖挥距触星松送获兴独官混纪依未突架宽冬章湿偏纹吃执阀矿寨责熟稳夺硬价努翻奇甲预职评读背协损棉侵灰虽矛厚罗泥辟告卵箱掌氧恩爱停曾溶营终纲孟钱待尽俄缩沙退陈讨奋械载胞幼哪剥迫旋征槽倒握担仍呀鲜吧卡粗介钻逐弱脚怕盐末阴丰雾冠丙街莱贝辐肠付吉渗瑞惊顿挤秒悬姆烂森糖圣凹陶词迟蚕亿矩康遵牧遭幅园腔订香肉弟屋敏恢忘编印蜂急拿扩伤飞露核缘游振操央伍域甚迅辉异序免纸夜乡久隶缸夹念兰映沟乙吗儒杀汽磷艰晶插埃燃欢铁补咱芽永瓦倾阵碳演威附牙芽永瓦斜灌欧献顺猪洋腐请透司危括脉宜笑若尾束壮暴企菜穗楚汉愈绿拖牛份染既秋遍锻玉夏疗尖殖井费州访吹荣铜沿替滚客召旱悟刺脑措贯藏敢令隙炉壳硫煤迎铸粘探临薄旬善福纵择礼愿伏残雷延烟句纯渐耕跑泽慢栽鲁赤繁境潮横掉锥希池败船假亮谓托伙哲怀割摆贡呈劲财仪沉炼麻罪祖息车穿货销齐鼠抽画饲龙库守筑房歌寒喜哥洗蚀废纳腹乎录镜妇恶脂庄擦险赞钟摇典柄辩竹谷卖乱虚桥奥伯赶垂途额壁网截野遗静谋弄挂课镇妄盛耐援扎虑键归符庆聚绕摩忙舞遇索顾胶羊湖钉仁音迹碎伸灯避泛亡答勇频皇柳哈揭甘诺概宪浓岛袭谁洪谢炮浇斑讯懂灵蛋闭孩释乳巨徒私银伊景坦累匀霉杜乐勒隔弯绩招绍胡呼痛峰零柴簧午跳居尚丁秦稍追梁折耗碱殊岗挖氏刃剧堆赫荷胸衡勤膜篇登驻案刊秧缓凸役剪川雪链渔啦脸户洛孢勃盟买杨宗焦赛旗滤硅炭股坐蒸凝竟陷枪黎救冒暗洞犯筒您宋弧爆谬涂味津臂障褐陆啊健尊豆拔莫抵桑坡缝警挑污冰柬嘴啥饭塑寄赵喊垫丹渡耳刨虎笔稀昆浪萨茶滴浅拥穴覆伦娘吨浸袖珠雌妈紫戏塔锤震岁貌洁剖牢锋疑霸闪埔猛诉刷狠忽灾闹乔唐漏闻沈熔氯荒茎男凡抢像浆旁玻亦忠唱蒙予纷捕锁尤乘乌智淡允叛畜俘摸锈扫毕璃宝芯爷鉴秘净蒋钙肩腾枯抛轨堂拌爸循诱祝励肯酒绳穷塘燥泡袋朗喂铝软渠颗惯贸粪综墙趋彼届墨碍启逆卸航衣孙龄岭骗休借".$addChars;
break;
default :
// 默认去掉了容易混淆的字符oOLl和数字01,要添加请使用addChars参数
$chars='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.$addChars;
break;
}
if($len>10 ) {//位数过长重复字符串一定次数
$chars= $type==1? str_repeat($chars,$len) : str_repeat($chars,5);
}
if($type!=4) {
$chars = str_shuffle($chars);
$str = substr($chars,0,$len);
}else{
// 中文随机字
for($i=0;$i<$len;$i++){
$str.= $this->msubstr($chars, floor(mt_rand(0,mb_strlen($chars,'utf-8')-1)),1);
}
}
return $str;
}
/**
* 字符串截取,支持中文和其他编码
* @static
* @access public
* @param string $str 需要转换的字符串
* @param string $start 开始位置
* @param string $length 截取长度
* @param string $charset 编码格式
* @param string $suffix 截断显示字符
* @return string
*/
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
if(function_exists("mb_substr"))
return mb_substr($str, $start, $length, $charset);
elseif(function_exists('iconv_substr')) {
return iconv_substr($str,$start,$length,$charset);
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
if($suffix) return $slice."…";
return $slice;
}
function output($im, $type='png', $filename='') {
header("Content-type: image/" . $type);
$ImageFun = 'image' . $type;
if (empty($filename)) {
$ImageFun($im);
} else {
$ImageFun($im, $filename);
}
imagedestroy($im);
}
}
/**
* 生成随机串
*
* @param int $len
* @return string
*/
function generate_code($len = 4)
{
$chars = '23457acefhkmprtvwxy';
for ($i = 0, $count = strlen($chars); $i < $count; $i++)
{
$arr[$i] = $chars[$i];
}
mt_srand((double) microtime() * 1000000);
shuffle($arr);
$code = substr(implode('', $arr), 5, $len);
return $code;
}
?>
一般可以直接使用,有问题,可以看下字体是否存在,字体路径是否正确