类实例_一个验证码类c_checkCodePic.php

来源:互联网 发布:淘宝管控记录下架 编辑:程序博客网 时间:2024/09/21 06:17

<?php
/*
*建立日期:2010年7月21日   9:04 
*建立人员:肖红阳
*功能说明:生成图片验证码和对应值的session"$_SESSION[check_code]"
*使用说明:
*     <?php
*      session_start();
*      include_once "../class/c_gd/c_checkCodePic.php";
*      $ccp=new c_checkCodePic('100','30','255','255 ','0','0','0','0','../font/simkai.ttf');
*     ?>  
*    100为宽,30为高,255,255,0,0,0,0为图片背景和字体三原色,./font/simkai.ttf为字体路径
*/
?>
<?php
session_start();
?>
<?php

class c_checkCodePic{
public $width;
public $height;
function __construct($width,$height,$bgcolor_r,$bgcolor_g,$bgcolor_b,$fontcolor_r,$fontcolor_g,$fontcolor_b,$fontstyle){
   $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->fontstyle=$fontstyle;      //文本字体路径
   $this->text_size=rand(11,13);      //文本大小值
   $this->text_x=rand(5,40);       //文本坐标X值
   $this->text_y=rand(15,27);       //文本坐标y值
   $this->rand_num=rand(1,9);       //随机数值
   $this->pixelcount=rand(100,120);     //干扰点的数量
   $this->dis_min=8;         //文字隔最小值
   $this->dis_max=18;         //文字隔最小值
   $this->angle=40;         //文字旋转最大角度
   $this->img=$this->createimg();      //建立画布并返回画布句柄
   imagecolorallocate($this->img,$this->bgcolor_r,$this->bgcolor_g,$this->bgcolor_b);   //填充背景色
   $this->rand_color=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255)); //设置随机颜色
   $this->getimage();
                     //输出图片
}
/******************************************************建立并返回设置好大小的画布******************************************/
function createimg(){
   $img=imagecreate($this->width,$this->height);
   return $img;
}
/***************************************计算并返回随机数,设置$_SESSION[check_code]值******************************************/
function getCheckCode(){
   for($i=0;$i<4;$i++){
    $str .= strtoupper(dechex(rand(1,15)));
   }
   $_SESSION[check_code]=$str;       //设置$_SESSION[check_code]值
   return $str;
}
/******************************************************建立图片******************************************/
function getimage(){
   $f_color = imagecolorallocate($this->img,$this->fontcolor_r,$this->fontcolor_g,$this->fontcolor_b); //设置文本色
   $str=$this->getCheckCode();                    //获取随机数
   $_SESSION[check_code]=$str;                    //将随机数赋值给$_SESSION[check_code]
   imagettftext($this->img,$this->text_size,rand(-9,$this->angle),$x=$this->text_x,$this->text_y,$f_color,$this->fontstyle,substr($str,0,1)); //将文本写入到画布
   imagettftext($this->img,$this->text_size,rand(-9,$this->angle),$x+=rand($this->dis_min,$this->dis_max),$this->text_y,$f_color,$this->fontstyle,substr($str,1,1));
   imagettftext($this->img,$this->text_size,rand(-9,$this->angle),$x+=rand($this->dis_min,$this->dis_max),$this->text_y,$f_color,$this->fontstyle,substr($str,2,1));
   imagettftext($this->img,$this->text_size,rand(-9,$this->angle),$x+=rand($this->dis_min,$this->dis_max),$this->text_y,$f_color,$this->fontstyle,substr($str,3,1));
   //布置干扰线
   //imageline($this->img, 0 ,rand(-10,50),rand(100,150),rand(0,30),imagecolorallocate($this->img,rand(0,225),rand(0,155),rand(0,155)));      //布置干扰线
   //imageline($this->img, 0 ,rand(-10,50),rand(100,150),rand(0,30),imagecolorallocate($this->img,rand(0,225),rand(0,155),rand(0,155)));      //布置干扰线
   //imageline($this->img, 0 ,rand(-10,50),rand(100,150),rand(0,30),imagecolorallocate($this->img,rand(0,225),rand(0,155),rand(0,155)));      //布置干扰线
   //布置干扰点
   for($i=0;$i<$this->pixelcount;$i++){
    imagesetpixel($this->img,rand(0,imagesx($this->img)),rand(0,imagesy($this->img)),imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255)));
   }
   header("Content-type: image/png");
   imagepng($this->img);                     //输出图片
}
}
?>

使用效果如下:

原创粉丝点击