生成图形认证的代码如下: StringAuth.php
[php]
<?php
/**
* @name 生成图形认证的类
* @version 0.1
* **/
class StringAuth{
private $width;
private $height;
private $len;
private $bgcolor;
private $noise;
private $noise_num;
private $border;
private $border_color;
private $image;
private $font_dir;
private $code; //认证码
/**
* @todo 完成必要的图形参数设置
* @param $width:高度
* @param $height
* @param $len 生成数字的个数
* @param $bgcolor:高度
* @param $noise 是否要干扰码 true | fasle
* @param $noise_num
* @param $border
* @param $border_color
* @param $font_dir
* */
function StringAuth($width,$height,$len,$bgcolor,
$noise,$noise_num,$border,$border_color,$font_dir){
$this->width = $width;
$this->height = $height;
$this->len = $len;
$this->bgcolor = $bgcolor;
$this->noise = $noise;
$this->noise_num= $noise_num;
$this->border = $border;
$this->border_color = $border_color;
$this->font_dir = $font_dir;
$this->AuthInit();
}
/**
* @return 数字图形
* */
function AuthInit(){
session_start();
$this->image = imageCreate($this->width, $this->height);
$back = $this->getcolor($this->bgcolor);
imageFilledRectangle($this->image, 100, 300, $this->width, $this->height, $back);
$size = $this->width/$this->len;
if($size > $this->height){
$size = $this->height;
}
$left = ($this->width-$this->len*($size+$size/10))/$size;
for ($i = 0; $i < $this->len; $i++)
{
$randtext = rand(0, 9);
$code .= $randtext;
$textColor = imageColorAllocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
$font = $this->font_dir.rand(1,4).".ttf";
$randsize = rand($size-$size/10, $size+$size/10);
$location = $left+($i*$size+$size/10);
imagettftext($this->image, $randsize, rand(-18,18), $location, rand($size-$size/10, $size+$size/10), $textColor, $font, $randtext);
}
if($this->noise == true){
$this->setnoise();
}
$_SESSION['code'] = $code;
$bordercolor = $this->getcolor($this->bordercolor);
if($this->border==true){
imageRectangle($this->image, 0, 0, $this->width-1, $this->height-1, $bordercolor);
}
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", true);
// HTTP/1.0
header("Pragma: no-cache");
if (function_exists("imagepng"))
{
header("Content-type: image/png");
imagepng($this->image);
} elseif (function_exists("imagegif"))
{
header("Content-type: image/gif");
imagegif($this->image);
} elseif (function_exists("imagejpeg"))
{
header("Content-type: image/jpeg");
imagejpeg($this->image);
} else
{
die("No image support in this PHP server!");
}
imagedestroy ($this->image);
}
/**
* @param $color
* @return $color
* ***/
function getcolor($color)
{
$color = eregi_replace ("^#","",$color);
$r = $color[0].$color[1];
$r = hexdec ($r);
$b = $color[2].$color[3];
$b = hexdec ($b);
$g = $color[4].$color[5];
$g = hexdec ($g);
$color = imagecolorallocate ($this->image, $r, $b, $g);
return $color;
}
function setnoise()
{
for ($i = 0; $i < $this->noise_num; $i++){
$randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
imageSetPixel($this->image, rand(0, $this->width), rand(0, $this->height), $randColor);
}
}
}
$x = new StringAuth(200,70,4,"#ffffff",true,400,true,"#cccccc","./font/");
?>
[/php]
引用的代码如下:
[php]
<?php
session_start();
?>
 |
<?php
echo $_SESSION['code'];
?>
[/php]
测试输出的图形数字和SESSSION的不一样, 而是落后一个节拍
但当
[php]
if (function_exists("imagepng"))
{
header("Content-type: image/png");
imagepng($this->image);
} [/php]
改为有图片存储, 而引用代码只引用存储的图片
[php]if (function_exists("imagepng"))
{
header("Content-type: image/png");
imagepng($this->image,"temp.png");
} [/php]
而引用代码为
[php]
<?php
session_start();
include("StringAuth.php");
?>
 |
<?php
echo $_SESSION['code'];
?>
[/php]
请问下, 到底是哪里出了问题? 还是输出缓冲有问题