//include_once(ROOT.'gb_php/passport.class.php');
//引入生成图片验证码类
$vcode = new Vcode();
$vcode->setLength(4);
//$vcode->setWidth(200);
//$vcode->setHeight(88);
$vcode->setShowBorder(false);
$vcode->setBgColor('#ffffff');
$vcode->setFontColor('#6565FE');
//$vcode->setFontSize(50);
$vcode->setFontSize(30);
$vcode->setDotNoise(40);
$vcode->setLineNoise(2);
//返回验证码图片IO流
$rs = $vcode->paint();
$base64 = chunk_split(base64_encode($rs['imgIo']));
$encode = '<img src="data:image/jpg/png/gif;base64,'.$base64.'">';
$return['img'] = $base64;
$return['code'] = $rs['md5Code'];
<?php
/**
* Verification Code Class
*
* Used to anti-spam base at PHP GD Lib
* @author Eric,<wangyingei@yeah.net>
* @version 1.0
* @copyright Ereesoft Inc. 2009
* @update 2009-05-14 22:32:05
* @example
* session_sratr();
* $vcode = new Vcode();
* $vcode->setLength(5);
* $_SESSION['passport'] = $vcode->paint();// To be encrypted by MD5
* @update bfire.lai 2015-07-08
*/
class Vcode{
/**
* @var $width The width of the image,auto Calculated 验证图片宽度,程序自动计算
*/
private $width;
/**
* @var $height Image height 验证图片高度
*/
private $height;
/**
* @var $length Verification Code lenght 验证码长度
*/
private $length;
/**
* @var $bgColor Image background color default random 验证图片背景色
*/
private $bgColor;
/**
* @var $fontColor The text color 验证码颜色
*/
private $fontColor;
/**
* @var $fontSize The text size 验证码字体大小
*/
private $fontSize;
/**
* @var $showBorder 是否要边框
*/
private $showBorder;
/**
* @var $dotNoise The number of noise 噪点数量
*/
private $dotNoise;
/**
* @var $lineNoise The number of noise lines 干扰线数量
*/
private $lineNoise;
/**
* @var $im image resource GD图像操作资源
*/
private $im;
private $result;
/**
* void Vcode::Vcode()
*
* The constructor
*/
public function Vcode(){
$this->dotNoise = 40;//初始化噪点数量
$this->lineNoise = 0;//初始化干扰线数量
}
/**
* void Vcode::setLength(integer $length)
*
* Set Verification Code length
* @access public
* @param integer $length;
* @return void
*/
public function setLength($length){
$this->length = $length;
}
/**
* void Vcode::setBgColor(string $bgColor)
*
* Set background color of the Verification Image
* @access public
* @param string $bgColor e.g.: #ffffff;可以直接使用css书写中的16进制写法,但不可简写
* @return void
*/
public function setBgColor($bgColor){
$this->bgColor = sscanf($bgColor, '#%2x%2x%2x');
}
/**
* void Vcode::setFontColor(string $fontgColor)
*
* Set text color of the Verification Image
* @access public
* @param string $fontColor e.g.: #ffffff;可以直接使用css书写中的16进制写法,但不可简写
* @return void
*/
public function setFontColor($fontColor){
$this->fontColor = sscanf($fontColor, '#%2x%2x%2x');
}
public function setFontSize($fontSize){
$this->fontSize = $fontSize;
}
//@设置是否显示边框
public function setShowBorder($border){
$this->showBorder=$border;
}
/**
* void Vcode::setDotNoise(integer $num)
*
* How many noise dots want to draw
* @access public
* @param integer $num Too much will lower performance
* @return void
*/
public function setDotNoise($num){
$this->dotNoise = $num;//手动设置噪点数量后,会覆盖初始设置
}
/**
* void Vcode::setLineNoise(integer $num)
*
* How many noise lines want to draw
* @access public
* @param integer $num Too much will lower performance
* @return void
*/
public function setLineNoise($num){
$this->lineNoise = $num;//手动设置干扰线数量后,会覆盖初始设置
}
/**
* void Vcode::setWidth(integer $num)
*
* How many noise lines want to draw
* @access public
* @param integer $num Too much will lower performance
* @return void
*/
public function setWidth($num){
$this->width = $num;//手动设置宽度
}
/**
* void Vcode::setHeight(integer $num)
*
* How many noise lines want to draw
* @access public
* @param integer $num Too much will lower performance
* @return void
*/
public function setHeight($num){
$this->height = $num;//手动设置高度
}
/**
* String Vcode::randString()
*
* Create Random characters 生成随机字符串
* @access private
* @return String
*/
private function randString( ){
//$string = strtoupper(md5(microtime().mt_rand(0,9)));
$a = new createnumber($this->length);
$res = $a->createcode();
$str = explode('|',$res);
$this->result = $str[0];
return $str[1];
// return substr($string, 0, $this->length);
}
/**
* void Vcode::drawDot()
*
* Draw dots noise 根据制定的数量随机画噪点,噪点颜色也随机
* @access private
*/
private function drawDot(){
for($i=0; $i<$this->dotNoise; $i++){
$color = imagecolorallocate($this->im,
101,
101,
254);//生成随机颜色
imagesetpixel($this->im,
mt_rand(0,$this->width),
mt_rand((($this->height)/2),$this->height),
$color);//在随机生成的坐标上画噪点
}
}
/**
* void Vcode::drawLine()
*
* Draw line noise 随机颜色随机画干扰线
* @access private
*/
private function drawLine(){
for($i=0; $i<$this->lineNoise; $i++){
$color = imagecolorallocate($this->im,
101,
101,
254);//随机生成颜色
imagesetthickness($this->im,3);
imageline($this->im,
mt_rand(0,(($this->width)/4)),
mt_rand(0,(($this->height)/4)),
mt_rand((($this->width)/4),$this->width),
mt_rand((($this->width)/4),(($this->height)/2)),
$color);//在随机生成的坐标上画干扰线
}
}
/**
* String Vcode::paint()
*
* Create image and output
* @access public
* @return string The Verification Code to be encrypted by MD5
*/
public function paint(){
if(empty($this->length)) $this->length = 8;//验证码默认长度为8
if(empty($this->fontSize)) $this->fontSize=6;
if(empty($this->width)) $this->width = 150 ;//计算验证图片宽度
if(empty($this->height)) $this->height =50;//制定验证码图片高度
$this->im = imagecreate($this->width, $this->height);//创建画布
if(empty($this->bgColor) || empty($this->fontColor)){//如果没有设置前景色和背景色则全部随机
//避免前景色和背景色过于接近,背景色(0-130)的随机范围与前景色(131-255)分开
imagecolorallocate( $this->im,
mt_rand(0,130),
mt_rand(0,130),
mt_rand(0,130));
$randString = $this->randString();
for($i=0; $i<$this->length; $i++){
$fontColor = imagecolorallocate($this->im,
mt_rand(131,255),
mt_rand(131,255),
mt_rand(131,255));
imagettftext($this->im,
$this->fontSize,
10,
$i*($this->fontSize-5)+10,
mt_rand(30,35),
$fontColor,
'fonts/arial.ttf',
$randString{$i});
//单个验证码字符高度随机,避免被OCR
}
} else {//如果设置了背景色和前景色,则不使用随机颜色
imagecolorallocate( $this->im,
$this->bgColor[0],
$this->bgColor[1],
$this->bgColor[2]);
$randString = $this->randString();
$fontColor = imagecolorallocate($this->im,
$this->fontColor[0],
$this->fontColor[1],
$this->fontColor[2]);
for($i=0;$i<$this->length;$i++){
if($i==4){
$angle = 0;
}else{
$angle = mt_rand(-30,30);
}
imagettftext($this->im,
$this->fontSize,
$angle,
$i*($this->fontSize-5)+10,
mt_rand(30,35),
$fontColor,
'img/fonts/arial.ttf',
$randString{$i});//每个验证码字符高度仍然随机
/*
//echo $this->im;
echo $this->fontSize;
echo "<br>";
echo $i*10+10;
echo "<br>";
echo mt_rand(0,4);
echo "<br>";
echo $fontColor;
echo "<br>";
if(!is_file('fonts/arial.ttf')){echo true;}
echo "<br>";
echo $randString{$i};
echo "<br>";
*/
}
}
$this->drawDot();//绘制噪点
$this->drawLine();//绘制干扰线
if($this->showBorder){imagerectangle($this->im,0,0,$this->width-1,$this->height-1,$fontColor);} //添加边框
// imagepng($this->im);
// imagedestroy($this->im);
// return md5($this->result);//返回MD5加密后的验证码,可直接放入session
// $imgIo ='code_tmp.png';
// imagepng($this->im,$imgIo);
// imagedestroy($this->im);
ob_start();
imagepng($this->im);
$content = ob_get_contents();
ob_end_clean();
$rs['imgIo'] = $content;
$rs['md5Code'] = md5($this->result);
return $rs;//返回MD5加密后的验证码,可直接放入session
}
}
class createnumber{
private $operator = '+-';
private $str1 ="";
private $str2 ="";
// function __construct() {
// $this->str1 = rand(2000,9000);
// $this->str2 = rand(1,9);
// }
//
// function createcode(){
// $ope1 = $this->operator{mt_rand(0,1)};
// $code1 = $this->str1;
// $code2 = $this->str2;
// $string = $code1.$ope1.$code2."=?";//生成字符串
// if($ope1 == '+'){
// $result = $this->str1+$this->str2;
// }else if($ope1 == '-'){
// $result = $this->str1-$this->str2;
// }
// return $result."|".$string ;
// }
function __construct($length) {
//$this->str1 = rand(2000,9000);
//$this->str2 = rand(1,9);
$str = 'qwertyuipasdfghjkzxcvbnm';
$len = strlen($str)-1;
for($i=0;$i<$length;$i++)
{
$num = mt_rand(0,$len);
$this->str1 = $this->str1.$str[$num];
}
}
function createcode(){
//$ope1 = $this->operator{mt_rand(0,1)};
$code1 = $this->str1;
//$code2 = $this->str2;
//$string = $code1.$ope1.$code2."=?";//生成字符串
$string = $code1;
$result = $code1;
// if($ope1 == '+'){
// $result = $this->str1+$this->str2;
// }else if($ope1 == '-'){
// $result = $this->str1-$this->str2;
// }
return $result."|".$string ;
}
}
?>