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();

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

class vCodeImage{
 var $mode;  //1:数字模式,2:字母模式,3:数字字母模式,其他:数字字母优化模式
 var $v_num;  //验证码个数
 var $img_w;  //验证码图像宽度
 var $img_h;  //验证码图像高度
 var $int_pixel_num;  //干扰像素个数
 var $int_line_num;  //干扰线条数
 var $font_dir;   //字体文件相对路径
 var $border;   //图像边框
 var $borderColor;  //图像边框颜色

function SetImage($made,$v_num,$img_w,$img_h,$int_pixel_num,$int_line_num,$font_dir='../font',$border=true,$borderColor='255,200,85'){
  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"){2881064151
   $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);
 }
}
?>

把验证码和生成时间负值给$_SESSION[vCode]生成图像给浏览器的更多相关文章

  1. DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate Date Dimension)

    跟大家的交流是我的动力. :) DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate Date Dimension) 难度: ★☆☆☆☆(1星) 适用范围: ★ ...

  2. captcha.js一个生成验证码的插件,使用js和canvas生成

    一.captcha`captcha.js`是一个生成验证码的插件,使用js和canvas生成的,确保后端服务被暴力攻击,简单判断人机以及系统的安全性,体积小,功能多,支持配置. 验证码插件内容,包含1 ...

  3. MySQL学习笔记:生成时间维度表

    # ---- 对应时间戳怎么生成的? ---- /*TIME_CD TIME_CD1000000 000005000001 000005000002 000005000003 000005000004 ...

  4. DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension)

    DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension) 难度: ★☆☆☆☆(1星) 适用范围: ★★★★★(5星) 这个时间系列想 ...

  5. oracle随机生成时间

    Julian Date定义 Julian Date:有效的日期范围是从公元前4713年1月1日到公元9999年12月31日.儒略日(julian date)是自公元前4713年1月1日中午12时起经过 ...

  6. 使用EF Code First生成模型,如何让时间字段由数据库自动生成

    场景:保存记录时需要时间字段,该时间如果由前台通过DateTime.Now产生,存在风险,比如修改客户端的系统时间,就会伪造该记录的生成时间.因此,需要在保存记录时,由后台自动赋予具体的时间. 实现方 ...

  7. 卷积生成对抗网络(DCGAN)---生成手写数字

    深度卷积生成对抗网络(DCGAN) ---- 生成 MNIST 手写图片 1.基本原理 生成对抗网络(GAN)由2个重要的部分构成: 生成器(Generator):通过机器生成数据(大部分情况下是图像 ...

  8. C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用

    1.直接调用 已知webservice路径,则可以直接 添加服务引用--高级--添加web引用 直接输入webservice URL.这个比较常见也很简单 即有完整的webservice文件目录如下图 ...

  9. wsdl自动生成Java代码,根据wsdl生成Java代码

    wsdl自动生成Java代码,根据wsdl生成Java代码 >>>>>>>>>>>>>>>>>&g ...

随机推荐

  1. JavaScript设计模式——工厂模式

    工厂模式:是一种实现“工厂”概念的面上对象设计模式.实质是定义一个创建对象的接口,但是让实现这个接口的类来决定实例化哪个类.工厂方法让类的实例化推迟到子类中进行.创建一个对象常常需要复杂的过程,所以不 ...

  2. hdu5119 dp

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5119 题意: 输入T组数据,每组数据包括两个数n和m,接下来n个数,这n个数可以随意取( ...

  3. 07 JavaWeb

    软件开发的两种架构:c/s和b/s          * C/S     client/server     客户端/服务器     例子:QQ     快播     暴风影音...          ...

  4. 期望+DP ZOJ 3929 Deque and Balls

    题目链接 题意:给你n个数,按照顺序依次放入一个双端队列(可放在头部,也可以放在尾部),求xi > xi+1的期望 * 2^n mod (1e9 +7) 分析:期望*2^n=出现这种排法的概率* ...

  5. header元素

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. BZOJ 3223 & 区间翻转

    题意: 就是贴个代码,这是我入门题的弱化版..然而一共还是写了40分钟,不专注(一边看比赛一边打)是一个问题,splay每个操作的细节确实有点多(什么时候updata啊..什么时候pushdown啊. ...

  7. border单样式写法的问题

    先写 border-top:5px; border-right:10px; 后写: border-style:solid; border-color:red;

  8. ACM : Travel-并查集-最小生成树 + 离线-解题报告

    Travel Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u /*题意 给出n[节点 ...

  9. ACM: 敌兵布阵 解题报告 -线段树

    敌兵布阵 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Li ...

  10. git两种合并方法 比较merge和rebase

    18:01 2015/11/18git两种合并方法 比较merge和rebase其实很简单,就是合并后每个commit提交的id记录的顺序而已注意:重要的是如果公司用了grrit,grrit不允许用m ...