+ -
当前位置:首页 → 问答吧 → 我也来发个验证码类,支持随机字体,样子还可以哦~

我也来发个验证码类,支持随机字体,样子还可以哦~

时间:2007-11-27

来源:互联网

原本这是我自己写的用在我网站上的一段程序,
不是CLASS也不是FUNCTION,到这里看看大家都是CLASS,
就改成了CLASS发上来了。以前没写过类。
这是我写的第一个类,写得不好还望大家指出!

还有就是想问下,我感觉在PHP中CLASS是没什么用处的,还不如function好用,效率也没function高,是不是这样的?


会根据你指定的图片大小改变文字大小,不过还不是很完善。



验证码示例:
1.gif (977 Bytes)
2007-11-27 11:52
2.gif (942 Bytes)
2007-11-27 11:52
3.gif (956 Bytes)
2007-11-27 11:52
4.gif (981 Bytes)
2007-11-27 11:52


调用示例:
复制PHP内容到剪贴板
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内容到剪贴板
PHP代码:

<?php
session_start();

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

class vCodeImage{
 /*******************************************************
 **FILENAME: verify_image.php
 **COPYRIGHT: NONE! (但请保留此信息)
 **AUTHOR: vsFree.Com
 **DATE: 2007-08-08
 ********************************************************/

 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"){
   $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);
 }
}
?>

[ 本帖最后由 vsfree 于 2007-11-27 12:19 编辑 ]

作者: vsfree   发布时间: 2007-11-27

确实不错

作者: PHPChina   发布时间: 2007-11-27

高手啊。。

作者: dzjzmj   发布时间: 2007-11-29

鼓励原创精品!

作者: yuen   发布时间: 2007-11-29

谢谢楼上几位

作者: luzhou   发布时间: 2007-12-01

5555555555555555555

作者: vsfree   发布时间: 2007-12-15

谢谢,很好的component

收藏起来

作者: leidong8   发布时间: 2007-12-25

谢谢楼主,现学现用了

作者: phpzg   发布时间: 2007-12-29

强烈支持!

作者: 凌云战神   发布时间: 2007-12-29

留名先

作者: fyland   发布时间: 2008-01-09

很好

作者: 痞子PHP   发布时间: 2008-01-09

楼主,我调了半天,也没有调出图像来呀???

作者: adleyliu   发布时间: 2008-01-09

狂晕!!!!!!!!!!!

找到毛病了!!!!

<?php
这一行之前,不能有空行!有了,就不能显示出图片来....!!!!!搞了半天,原来是这里的事.

作者: 痞子PHP   发布时间: 2008-01-09

昵称: 痞子PHP  时间: 2008-1-10 13:17
   调用后怎么显示的是一个不见东西的坏图片呢 !

作者: 痞子PHP   发布时间: 2008-01-10

强烈支持!

作者: 312344505   发布时间: 2008-01-10

作者: 312344505   发布时间: 2008-01-15

有些同学不能用可能是PHP4版本的!

用以下代码即可解决!
复制PHP内容到剪贴板
PHP代码:

<?php
session_start();
$vi = new vCodeImage;
$vi->SetImage(3,4,80,25,40,1);
class vCodeImage{
 /*******************************************************
 **FILENAME: verify_image.php
 **COPYRIGHT: NONE! (但请保留此信息)
 **AUTHOR: vsFree.Com
 **DATE: 2007-08-08
**Modify: jason.chu
 ********************************************************/
 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"){
   $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, 255,255,255); 
  imagefill($im,0,0,$bgcolor);
  $fonts = $this->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(100,200,100,200,100,200);
   $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); //右竖
  }

  $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);
 }
 function ScanDir($dir){
 $dh  = opendir($dir);
 while (false !== ($filename = readdir($dh))) {
     $files[] = $filename;
 }
 sort($files);
 return $files;
 }
}
?>

作者: strongability   发布时间: 2008-01-15

学习了~

作者: strongability   发布时间: 2008-05-27

Warning: imagegif() [function.imagegif]: open_basedir restriction in effect. File(C:\WINDOWS\TEMP\) is not within the allowed path(s): (D:/www/;D:/APMServ5.2.0/PHP/uploadtemp/;D:/APMServ5.2.0/PHP/sessiondata/) in D:\www\taocheng\htdoc\verify_image.php on line 135

Warning: imagegif() [function.imagegif]: Unable to open temporary file in D:\www\taocheng\htdoc\verify_image.php on line 135

作者: qingis   发布时间: 2008-05-28

上去

作者: richardhc   发布时间: 2008-05-31

我的是字体文件没有下载,所有问题。

作者: sanler   发布时间: 2008-06-09

昵称: diego  时间: 2008-6-23 09:48
这些代码怎么用它,才能看到效果

作者: Alog_W   发布时间: 2008-06-23

不错,收下了。

作者: diego   发布时间: 2008-06-23

和书上的一个例子一样

不过改成类了

作者: diego   发布时间: 2008-07-02

很 漂亮 先谢了

作者: juehan1314   发布时间: 2008-07-03

呵呵很好谢谢

作者: d9tx   发布时间: 2008-07-03