我想写个图片验证码类,可是引用的时候图片显示不出来,谁能帮帮我啊?
<?php
/*
Created on 2010-7-20
创建随机数-> 写入session ->写入图片
图片验证码类checkCodePic
*/
class c_checkCodePic{
public $width;
public $height;
public $bgcolor_r;
public $bgcolor_g;
public $bgcolor_b;
public $fontcolor_r;
public $fontcolor_g;
public $fontcolor_b; function __construct($width,$height,$bgcolor_r,$bgcolor_g,$bgcolor_b,$fontcolor_r,$fontcolor_g,$fontcolor_b){
session_start();//启用session
$this->width=$width; //画布宽度
$this->height=$height; //画布高度
$this->bgcolor_r=$bgcolor_r; //画布颜色red值
$this->bgcolor_g=$bgcolor_g; //画布颜色green值
$this->bgcolor_b=$bgcolor_b; //画布颜色blue值
$this->fontcolor_r=$fontcolor_r; //文本颜色red值
$this->fontcolor_g=$fontcolor_g; //文本颜色green值
$this->fontcolor_b=$fontcolor_b; //文本颜色blue值
$this->font=rand(2,6); //文本字体
$this->text_x=rand(1,60); //文本x坐标
$this->text_y=rand(1,15); //文本y坐标
$this->getImage(); //输出图片
}
/******************************获取随机码并创建$_SESSION[check]=$rand**********************************/
function getSession(){
for($i=0;$i<4;$i++){
$rand.= dechex(rand(1,15));
}
$_SESSION[check]=$rand; //写入session
return $rand;
}
/*****************************************设置图像**********************************/
function setImage(){
$img=imagecreate ($this->width,$this->height);
$bg = imagecolorallocate($img,$this->bgcolor_r,$this->bgcolor_g,$this->bgcolor_b); //第一次用调试版的时候是背景颜色
$font_c=imagecolorallocate($img,$this->fontcolor_r,$this->fontcolor_g,$this->fontcolor_b); //设置字体颜色
imagestring($img,$this->font,$this->text_x,$this->text_y,$this->getSession(),$font_c); //将字符串写在图像上面
return $img;
}
/*****************************************输出图像**********************************/
function getImage(){
header ("Content-type: image/png"); //定义文件格式
imagepng($this->setImage()); //定义编码方式
}
}
?>

解决方案 »

  1.   


    /**//**变量最好预定义一下**/
    function getSession(){
    $rand='';
    for($i=0;$i<4;$i++){
    $rand .= dechex(rand(1,15));
    }
    $_SESSION['check']=$rand;  //下标''
    return $rand;
    }
    $pic  = new c_checkCodePic(120,50,'255','255','255','255','00','00');
    $pic->getImage();
      

  2.   


    构造函数已经调用 getImage 了,后面不需要在调用了吧。
      

  3.   

    非常感谢,不过PHP中变量不用预先定义的呀,在用于字符串时它默认为空字符串' '的吧