图形验证码类,完全定制化

php
图形验证码类
<img src=”verify_image.php” alt=”点此刷新验证码” name=”verify_code”
width=”65″ height=”20″ border=”0″ id=”verify_code”
onclick=”document.getElementById(‘verify_code’).src=’verify_image.php?’+Math.random();”
style=”cursor:pointer;” />

<?php教程
session_start();
class authnum {
//图片对象、宽度、中度、验证码长度
private $im;
private $im_width;
private $im_height;
private $len;
//随机字符串、y轴坐标值、随机颜色
private $randnum;
private $y;
private $randcolor;
//背景象的红青灰,暗中同意是浅奶油色
public $red=238;
public $green=238;
public $blue=238;
/**
* 可选设置:验证码类型、苦恼点、苦恼线、y轴随机
* 设为 false 表示不启用
**/
//私下认可是大大小小写数字混合型,1 2 3 分别表示 小写、大写、数字型
public $ext_num_type=”;
public $ext_pixel = false; //干扰点
public $ext_line = false; //干扰线
public $ext_rand_y= true; //y轴随机
function __construct ($len=4,$im_width=”,$im_height=25) {
// 验证码长度、图片宽度、高度是实例化类时必需的数据
$this->len = $len; $im_width = $len * 15;
$this->im_width = $im_width;
$this->im_height= $im_height;
$this->im = imagecreate($im_width,$im_height);
}
// 设置图片背景颜色,私下认可是浅青莲背景
function set_bgcolor () {
imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
}
// 得到人身自由位数的随机码
function get_randnum () {
$an1 = ‘abcdefghijklmnopqrstuvwxyz’;
$an2 = ‘abcdefghijklmnopqrstuvwxyz’;
$an3 = ‘0123456789’;
if ($this->ext_num_type == ”) $str = $an1.$an2.$an3;
if ($this->ext_num_type == 1) $str = $an1;
if ($this->ext_num_type == 2) $str = $an2;
if ($this->ext_num_type == 3) $str = $an3;
for ($i = 0; $i < $this->len; $i++) {
$start = rand(1,strlen($str) – 1);
$randnum .= substr($str,$start,1);
}
$this->randnum = $randnum;
$_session[an] = $this->randnum;
}
// 获得验证码图片y轴
function get_y () {
if ($this->ext_rand_y) $this->y = rand(5,
$this->im_height/5);
else $this->y = $this->im_height / 4 ;
}

Authnum.class.php
下载

复制代码字体文件见附属类小部件!

 

1、发生一张png的图形
2、为图片设置背景象
3、设置字体颜色和体裁
4、发生4位数的妄动的验证码
5、把发生的各样字符调治旋转角度和地方画到png图片上
6、参加噪点和搅扰线制止注册机器剖判原图片来恶意注册
7、输出图片
8、释放图片所占内部存款和储蓄器
authcode.php文件

复制代码 代码如下:

<?php

// 得到随机色
function get_randcolor () {
$this->randcolor =
imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));
}
// 增加苦恼点
function set_ext_pixel () {
if ($this->ext_pixel) {
for($i = 0; $i < 100; $i++){
$this->get_randcolor();
imagesetpixel($this->im, rand()%100, rand()%100,
$this->randcolor);
}
}
}
// 加多干扰线
function set_ext_line () {
if ($this->ext_line) {
for($j = 0; $j < 2; $j++){
$rand_x = rand(2, $this->im_width);
$rand_y = rand(2, $this->im_height);
$rand_x2 = rand(2, $this->im_width);
$rand_y2 = rand(2, $this->im_height);
$this->get_randcolor();
imageline($this->im, $rand_x, $rand_y, $rand_x2, $rand_y2,
$this->randcolor);
}
}
}
/**创设验证码图像:
* 创立画布(__construct函数)
* 设置画布背景($this->set_bgcolor();)
* 获取随机字符串($this->get_randnum ();)
* 文字写到图片上(imagestring函数)
* 增加干扰点/线($this->set_ext_line();
$this->set_ext_pixel();)
* 输出图片
**/
function create () {
$this->set_bgcolor();
$this->get_randnum ();
for($i = 0; $i < $this->len; $i++){
$font = rand(4,6);
$x = $i/$this->len * $this->im_width + rand(1,
$this->len);
$this->get_y();
$this->get_randcolor();
imagestring($this->im, $font, $x, $this->y,
substr($this->randnum, $i ,1), $this->randcolor);
}
$this->set_ext_line();
$this->set_ext_pixel();
header(“content-type:image/png”);
imagepng($this->im);
imagedestroy($this->im); //释放图像财富
}
}//end class
/**运用验证码类的方法:
* $an = new authnum(验证码长度,图片宽度,图片中度);
* 实例化时不带参数则暗中认可是肆位的60*25尺码的正规验证码图片
* 表单页面检验验证码的形式,相比较 $_session[an] 是或不是等于
$_post[验证码文本框id]
* 可选配置:
* 1.验证码类型:$an->ext_num_type=1;
值为1是小写类型,2是大写类型,3是数字类型
* 2.干扰点:$an->ext_pixel = false; 值为false表示不增加苦恼点
* 3.干扰线:$an->ext_line = false; 值为false表示不增多苦恼线
*图形验证码类,完全定制化。 4.y轴随机:$an->ext_rand_y = false;
值为false表示不协理图片y轴随机
* 5.图片背景:改换 $red $green $blue 四个成员变量的值就能够
**/
$an = new authnum();
$an->ext_num_type=”;
$an->ext_pixel = true; //干扰点
$an->ext_line = false; //干扰线
$an->ext_rand_y= true; //y轴随机
$an->green = 238;
$an->create();
?>

代码

<?php
session_start();
class Authnum {
//图片对象、宽度、高度、验证码长度
private $im;
private $im_width;
private $im_height;
private $len;
//随机字符串、y轴坐标值、随机颜色
private $randnum;
private $y;
private $randcolor;
//背景象的红煤黑,暗许是浅粉红
public $red=238;
public $green=238;
public $blue=238;
/**
* 可选设置:验证码类型、搅扰点、困扰线、Y轴随机
* 设为 false 表示不启用
**/
//私下认可是大大小小写数字混合型,1 2 3 分别表示 小写、大写、数字型
public $ext_num_type=”;
public $ext_pixel = false; //干扰点
public $ext_line = false; //干扰线
public $ext_rand_y= true; //Y轴随机
function __construct ($len=4,$im_width=”,$im_height=25) {
// 验证码长度、图片宽度、中度是实例化类时少不了的多寡
$this->len = $len; $im_width = $len * 15;
$this->im_width = $im_width;
$this->im_height= $im_height;
$this->im = imagecreate($im_width,$im_height);
}
// 设置图片背景颜色,暗许是浅日光黄背景
function set_bgcolor () {
imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
}
// 得到自由位数的随机码
function get_randnum () {
$an1 = ‘abcdefghijklmnopqrstuvwxyz’;
$an2 = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
$an3 = ‘0123456789’;
if ($this->ext_num_type == ”) $str = $an1.$an2.$an3;
if ($this->ext_num_type == 1) $str = $an1;
if ($this->ext_num_type == 2) $str = $an2;
if ($this->ext_num_type == 3) $str = $an3;
for ($i = 0; $i < $this->len; $i++) {
$start = rand(1,strlen($str) – 1);
$randnum .= substr($str,$start,1);
}
$this->randnum = $randnum;
$_SESSION[an] = $this->randnum;
}
// 获得验证码图片Y轴
function get_y () {
if ($this->ext_rand_y) $this->y = rand(5,
$this->im_height/5);
else $this->y = $this->im_height / 4 ;
}
// 获得随机色
function get_randcolor () {
$this->randcolor =
imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));
}
// 增多困扰点
function set_ext_pixel () {
if ($this->ext_pixel) {
for($i = 0; $i < 100; $i++){
$this->get_randcolor();
imagesetpixel($this->im, rand()%100, rand()%100,
$this->randcolor);
}
}
}
// 增添困扰线
function set_ext_line () {
if ($this->ext_line) {
for($j = 0; $j < 2; $j++){
$rand_x = rand(2, $this->im_width);
$rand_y = rand(2, $this->im_height);
$rand_x2 = rand(2, $this->im_width);
$rand_y2 = rand(2, $this->im_height);
$this->get_randcolor();
imageline($this->im, $rand_x, $rand_y, $rand_x2, $rand_y2,
$this->randcolor);
}
}
}
/**创造验证码图像:
* 创立画布(__construct函数)
* 设置画布背景($this->set_bgcolor();)
* 获取随机字符串($this->get_randnum ();)
* 文字写到图片上(imagestring函数)
* 增加干扰点/线($this->set_ext_line();
$this->set_ext_pixel();)
* 输出图片
**/
function create () {
$this->set_bgcolor();
$this->get_randnum ();
for($i = 0; $i < $this->len; $i++){
$font = rand(4,6);
$x = $i/$this->len * $this->im_width + rand(1, $this->len);
$this->get_y();
$this->get_randcolor();
imagestring($this->im, $font, $x, $this->y,
substr($this->randnum, $i ,1), $this->randcolor);
}
$this->set_ext_line();
$this->set_ext_pixel();
header(“content-type:image/png”);
imagepng($this->im);
imagedestroy($this->im); //释放图像能源
}
}//end class
/**动用验证码类的主意:
* $an = new Authnum(验证码长度,图片宽度,图片中度);
* 实例化时不带参数则私下认可是几位的60*25尺码的不荒谬验证码图片
* 表单页面检查实验验证码的主意,比较 $_SESSION[an] 是还是不是等于
$_POST[验证码文本框ID]
* 可选配置:
* 1.验证码类型:$an->ext_num_type=1;
值为1是小写类型,2是大写类型,3是数字类型
* 2.干扰点:$an->ext_pixel = false; 值为false表示不增多干扰点
* 3.干扰线:$an->ext_line = false; 值为false表示不增多苦恼线
* 4.Y轴随机:$an->ext_rand_y = false;
值为false表示不帮忙图片Y轴随机
* 5.图片背景:改动 $red $green $blue 多少个成员变量的值就可以
**/
$an = new Authnum();
$an->ext_num_澳门葡京备用网址,type=”;
$an->ext_pixel = true; //干扰点
$an->ext_line = false; //干扰线
$an->ext_rand_y= true; //Y轴随机
$an->green = 238;
$an->create();
?>

session_start();

 代码如下

您或者感兴趣的篇章:

  • php封装的验证码工具类完整实例
  • 八个能够的php验证码类(分享)
  • 二个经文的PHP验证码类共享
  • 多个好用的PHP验证码类实例分享
  • PHP
    动态随机生成验证码类代码
  • 三个PHP验证码类代码共享(已打包成类)
  • 转变随机字符串和验证码的类的PHP实例
  • php完结的Captcha验证码类实例
  • php完结的click
    captcha点击验证码类实例
  • PHP完结的封装验证码类详解
  • PHP封装的验证码工具类定义与用法示例

 

<?php
        session_start ();
        header ( ‘Content-type: image/png’ );
        //创造图片
        $im = imagecreate($x=130,$y=45 );
        $bg =
imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155)); //第叁遍对
imagecolorallocate() 的调用会给基于调色板的图像填充背景观
        $fontColor = imageColorAllocate ( $im, 255, 255, 255 );  
//字体颜色
        $fontstyle = ‘rock.ttf’;                  
//字体样式,这些能够从c:windowsFonts文件夹下找到,小编把它放到和authcode.php文件同叁个索引,这里能够替换别的的书体样式
        //爆发随机字符
        for($i = 0; $i < 4; $i ++) {
                $randAsciiNumArray         = array
(rand(48,57),rand(65,90));
                $randAsciiNum                 = $randAsciiNumArray
[rand ( 0, 1 )];
                $randStr                         = chr ( $randAsciiNum
);
               
imagettftext($im,30,rand(0,20)-rand(0,25),5+$i*30,rand(30,35),$fontColor,$fontstyle,$randStr);
                $authcode                        .= $randStr;
        }
        $_SESSION[‘authcode’]        =
$randFourStr;//用户和用户输入的验证码做比较
        //干扰线
        for ($i=0;$i<8;$i++){
                $lineColor        =
imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
                imageline ($im,rand(0,$x),0,rand(0,$x),$y,$lineColor);
        }
        //干扰点
        for ($i=0;$i<250;$i++){
                imagesetpixel($im,rand(0,$x),rand(0,$y),$fontColor);
        }
        imagepng($im);
        imagedestroy($im);               
?>

$vi = new vCodeImage;

例2

$vi->SetImage(1,4,65,20,80,1);

php验证码” alt=”” src=”/notfound.jpg” width=”499″ height=”196″ />

 

•新建一个PHP文件captcha_code_file.php

class vCodeImage{

 代码如下

 /*******************************************************

//首先开启session
session_start();
//定义前台展现验证码长&宽
$image_width = 120;
$image_height = 40;
$characters_on_image = 6;
$font = ‘./monofont.ttf’;
 
//The characters that can be used in the CAPTCHA code.
//avoid confusing characters (l 1 and i for example)
$possible_letters = ‘23456789bcdfghjkmnpqrstvwxyz’;
$random_dots = 10;
$random_lines = 30;
$captcha_text_color=”0x142864″;
$captcha_noice_color = “0x142864”;
//定义要生成验证码的字符串
$code = ”;
 
$i = 0;
while ($i < $characters_on_image) { 
$code .=
substr($possible_letters,
mt_rand(0, strlen($possible_letters)-1), 1);
$i++;
}
 
$font_size = $image_height * 0.75;
$image = @imagecreate($image_width, $image_height);
 
/* setting the background, text and noise colours here */
$background_color = imagecolorallocate($image, 255, 255, 255);
 
$arr_text_color = hexrgb($captcha_text_color);
$text_color = imagecolorallocate($image, $arr_text_color[‘red’], 
        $arr_text_color[‘green’], $arr_text_color[‘blue’]);
 
$arr_noice_color = hexrgb($captcha_noice_color);
$image_noise_color = imagecolorallocate($image,
$arr_noice_color[‘red’], 
        $arr_noice_color[‘green’], $arr_noice_color[‘blue’]);
 
/* generating the dots randomly in background */
for( $i=0; $i<$random_dots; $i++ ) {
imagefilledellipse($image,
mt_rand(0,$image_width),
 mt_rand(0,$image_height), 2, 3, $image_noise_color);
}
 
/* generating lines randomly in background of image */
for( $i=0; $i<$random_lines; $i++ ) {
imageline($image, mt_rand(0,$image_width),
mt_rand(0,$image_height),
 mt_rand(0,$image_width), mt_rand(0,$image_height),
$image_noise_color);
}
 
/* create a text box and add 6 letters code in it */
$textbox = imagettfbbox($font_size, 0, $font, $code); 
$x = ($image_width – $textbox[4])/2;
$y = ($image_height – $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font ,
$code);
 
/* Show captcha image in the page html page */
header(‘Content-Type: image/jpeg’);// defining the image type to be
shown in browser widow
imagejpeg($image);//showing the image
imagedestroy($image);//destroying the image instance
//设置session,做验证
$_SESSION[‘6_letters_code’] = $code;
 
function hexrgb ($hexstr)
{
  $int = hexdec($hexstr);
 
  return array(“red” => 0xFF & ($int >> 0x10),
               “green” => 0xFF & ($int >> 0x8),
               “blue” => 0xFF & $int);
}

 **FILENAME: verify_image.php

展现验证码页面index.php

 **COPYRIGHT: NONE! (但请保留此音信)

<?php
session_start();
if(isset($_REQUEST[‘Submit’])){
    // code for check server side validation
    if(empty($_SESSION[‘6_letters_code’] ) ||
        strcasecmp($_SESSION[‘6_letters_code’],
$_POST[‘6_letters_code’]) != 0)
    {  
        $msg=”您输入的验证码有误,请重新输入!”;
    }else{
        echo “您输入的是不易的!”;
        // Captcha verification is Correct. Final Code Execute here!
    }
}    
 ?>
 
<style type=”text/css”>
.table{
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
    color:#333;
    background-color:#E4E4E4;    
}
.table td{
    <a href=” Money point</a> 
background-color:#F8F8F8;     
}
</style>
 
<form action=”” method=”post” name=”form1″ id=”form1″ >
  <table width=”400″ border=”0″ align=”center” cellpadding=”5″
cellspacing=”1″>
<?php if(isset($msg)){?>
    <tr>
      <td colspan=”2″ align=”center” valign=”top”><?php echo
$msg;?></td>
    </tr>
<?php } ?> 
    <tr>
      <td align=”right” valign=”top”> 验证码Demo:</td>
      <td><img src=”captcha_code_file.php?rand=<?php echo
rand(0,20);?>” id=’captchaimg’  onclick=”refreshCaptcha();”
><br>
        <label for=’message’>请输入验证码:</label>
        <br>
        <input id=”6_letters_code” name=”6_letters_code”
type=”text”>
        <br>
        倘诺看不到,请 <a
href=’javascript:
refreshCaptcha();’>点作者</a> 刷新一下!
        </p></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input name=”Submit” type=”submit” onclick=”return
validate();” value=”Submit”></td>
    </tr>
  </table>
</form>
<script type=’text/javascript’>
//定义的基础代谢乞请
function refreshCaptcha()
{
    var img = document.images[‘captchaimg’];
    img.src =
img.src.substring(0,img.src.lastIndexOf(“?”))+”?rand=”+Math.random()*1000;
}
</script>

 **AUTHOR: vsFree.Com

例3

 **DATE: 2007-08-08

包罗雪花背景的验证码

 ********************************************************/

 代码如下

 

<?session_start();?>
<FORM METHOD=POST ACTION=””>
<input type=text name=number maxlength=4><img
src=”YanZhengMa.php?act=init”>
<INPUT TYPE=”submit” name=”sub”>
</FORM>
<?
//核准校验码
if(isset($HTTP_POST_VARS[“sub”])):
if($HTTP_POST_VARS[“number”] !=
$HTTP_SESSION_VARS[login_check_number] ||
empty($HTTP_POST_VARS[“number”])){
    echo “校验码不得法!” ;
}else{
    echo”验证码通过!”;
}
endif;
show_source(‘test.php’);
//以上本页的源码

 var $mode; 
//1:数字形式,2:字母形式,3:数字字母方式,别的:数字字母优化情势

//以下是生成验证码的源码
show_source(‘YanZhengMa.php’);
?>
<?php
session_start();
session_register(“login_check_number”);

 var $v_num;  //验证码个数

 

 var $img_w;  //验证码图像宽度

//前晚看看了chianren上的验证码功效,就思量了一下,用PHP的GD库实现了近乎意义
//先成生背景,再把变化的验证码放上去
$img_height=120;    //先定义图片的长、宽
$img_width=40;
if($HTTP_GET_VARS[“act”]== “init”){
    //srand(microtime() * 壹仟00);//PHP420后,srand不是必须的
    for($Tmpa=0;$Tmpa<4;$Tmpa++){
        $nmsg.=dechex(rand(0,15));
    }//by sports98

 var $img_h;  //验证码图像高度

    $HTTP_SESSION_VARS[login_check_number] = $nmsg;

 var $int_pixel_num;  //干扰像素个数

    //$HTTP_SESSION_VARS[login_check_number] =
strval(mt_rand(“1111″,”9999”));    //生成4位的即兴数,归入session中
    //哪个人能做下补充,能够何况生成字母和数字啊??—-由sports98到位了

 var $int_line_num;  //干扰线条数

 

 var $font_dir;   //字体文件相对路线

    $aimg = imageCreate($img_height,$img_width);    //生成图片
    ImageColorAllocate($aimg, 255,255,255);           
//图片底色,ImageColorAllocate第1次定义颜色PHP就以为是底色了
    $black = ImageColorAllocate($aimg, 0,0,0);        //定义供给的藕灰

 var $border;   //图像边框

   
ImageRectangle($aimg,0,0,$img_height-1,$img_width-1,$black);//先成一原野绿的矩形把图片包围

 var $borderColor;  //图像边框颜色

    //下边该生成雪花背景了,其实正是在图片上生成一些标志
    for ($i=1; $i<=100; $i++) {    //先用玖18个做测量试验

 

       
imageString($aimg,1,mt_rand(1,$img_height),mt_rand(1,$img_width),”*”,imageColorAllocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
       
//哈,看到了吗,其实亦不是白雪,就是生成*号而已。为了使它们看起来”一塌糊涂、5颜6色”,就得在1个1个变化它们的时候,让它们的任务、颜色,以致大小都用随机数,rand()或mt_rand都得以做到。
    }

 function
SetImage($made,$v_num,$img_w,$img_h,$int_pixel_num,$int_line_num,$font_dir=’../font’,$border=true,$borderColor=’255,200,85′){

   
//上边生成了背景,未来就该把早就成形的率性数放上来了。道理和方面大致,随机数1个1个地放,相同的时间让她们的地点、大小、颜色都用成随机数~~
    //为了分歧于背景,这里的颜料不超过200,下边包车型的士不低于200
    for
($i=0;$i<strlen($HTTP_SESSION_VARS[login_check_number]);$i++){
        imageString($aimg,
mt_rand(3,5),$i*$img_height/4+mt_rand(1,10),mt_rand(1,$img_width/2),
$HTTP_SESSION_VARS[login_check_number][$i],imageColorAllocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
    }
    Header(“Content-type: image/png”);   
//告诉浏览器,上面的数据是图表,而并不是按文字突显
    ImagePng($aimg);                   
//生成png格式。。。嘿嘿效果蛮像回事的呗。。。
    ImageDestroy($aimg);
}

  if(!isset($_SESSION[‘vCode’])){

?>

   session_register(‘vCode’);

  }

  $_SESSION[‘vCode’]=””;

 

  $this->mode = $made;

  $this->v_num = $v_num;

  $this->img_w = $img_w;

  $this->img_h = $img_h;

  $this->int_pixel_num = $int_pixel_num;

  $this->int_line_num = $int_line_num;

  $this->font_dir = $font_dir;

  $this->border = $border;

  $this->borderColor = $borderColor;

  $this->GenerateImage();

 }

 

 function GetChar($mode){

  if($mode == “1”){

   $ychar = “0,1,2,3,4,5,6,7,8,9”;

  }

  else if($mode == “2”){

   $ychar = “A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z”;

  }

  else if($mode == “3”){

   $ychar =
“0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z”;

  }

  else

   $ychar = “3,4,5,6,7,8,9,A,B,C,D,H,K,P,R,S,T,W,X,Y”;

  return $ychar;

 }

 

 function RandColor($rs,$re,$gs,$ge,$bs,$be){

  $r = mt_rand($rs,$re);

  $g = mt_rand($gs,$ge);

  $b = mt_rand($bs,$be);

  return array($r,$g,$b);

 }

 

 function GenerateImage(){

  $im = imagecreate($this->img_w,$this->img_h);

 

  $black = imagecolorallocate($im, 0,0,0);

  $white = imagecolorallocate($im, 255,255,255);

  $bgcolor = imagecolorallocate($im, 250,250,250);

 

  imagefill($im,0,0,$bgcolor);

 

  $fonts = ScanDir($this->font_dir);

  $fmax = count($fonts) – 2;

 

  $ychar = $this->GetChar($this->mode);

  $list = explode(“,”,$ychar);

 

  $x = mt_rand(2,$this->img_w/($this->v_num+2));

  $cmax = count($list) – 1;

 

  $v_code = ”;

 

  for($i=0;$i<$this->v_num;$i++) //验证码

  {

   $randnum = mt_rand(0,$cmax);

   $this_char = $list[$randnum];

   $v_code .= $this_char;

   $size =
mt_rand(intval($this->img_w/5),intval($this->img_w/4));

   $angle = mt_rand(-20,20);

   $y = mt_rand(($size+2),($this->img_h-2));

   if($this->border)

    $y = mt_rand(($size+3),($this->img_h-3));

   $rand_color = $this->RandColor(0,200,0,100,0,250);

   $randcolor =
imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);

   $fontrand = mt_rand(2, $fmax);

   $font = “$this->font_dir/”.$fonts[$fontrand];

   imagettftext($im, $size, $angle, $x, $y, $randcolor, $font,
$this_char);

   $x = $x + intval($this->img_w/($this->v_num+1));

  }

 

  for($i=0;$i<$this->int_pixel_num;$i++){//搅扰像素

   $rand_color = $this->RandColor(50,250,0,250,50,250);

   $rand_color_pixel =
imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);

   imagesetpixel($im, mt_rand()%$this->img_w,
mt_rand()%$this->img_h, $rand_color_pixel);

  }

 

  for($i=0;$i<$this->int_line_num;$i++){ //干扰线

   $rand_color = $this->RandColor(0,250,0,250,0,250);

   $rand_color_line =
imagecolorallocate($im,$rand_color[0],$rand_color[1],$rand_color[2]);

   imageline($im, mt_rand(0,intval($this->img_w/3)),
mt_rand(0,$this->img_h), mt_rand(intval($this->img_w –
($this->img_w/3)),$this->img_w), mt_rand(0,$this->img_h),
$rand_color_line);

  }

 

  if($this->border) //画出边框

  {

   if(preg_match(“/^d{1,3},d{1,3},d{1,3}$/”,$this->borderColor)){

    $borderColor = explode(‘,’,$this->borderColor);

   }

   $border_color_line =
imagecolorallocate($im,$borderColor[0],$borderColor[1],$borderColor[2]);

   imageline($im, 0, 0, $this->img_w, 0, $border_color_line);
//上横

   imageline($im, 0, 0, 0, $this->img_h, $border_color_line);
//左竖

   imageline($im, 0, $this->img_h-1, $this->img_w,
$this->img_h-1, $border_color_line); //下横

   imageline($im, $this->img_w-1, 0, $this->img_w-1,
$this->img_h, $border_color_line); //右竖

  }

 

  imageantialias($im,true); //抗锯齿

 

  $time = time();

  $_SESSION[‘vCode’] = $v_code.”|”.$time;
//把验证码和生成时间负值给$_SESSION[vCode]

 

  //生成图像给浏览器

  if (function_exists(“imagegif”)) {

      header (“Content-type: image/gif”);

      imagegif($im);

  }

  elseif (function_exists(“imagepng”)) {

      header (“Content-type: image/png”);

      imagepng($im);

  }

  elseif (function_exists(“imagejpeg”)) {

      header (“Content-type: image/jpeg”);

      imagejpeg($im, “”, 80);

  }

  elseif (function_exists(“imagewbmp”)) {

      header (“Content-type: image/vnd.wap.wbmp”);

      imagewbmp($im);

  }

  else

      die(“No Image Support On This Server !”);

 

  imagedestroy($im);

 }

}

 

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website