php 验证码程序(数字与字母)

 更新时间:2016年11月25日 16:57  点击:1778
一款php 验证码程序函数,原理是利用php gd库再生成随便数字,然后创建一个session与一张数字图片,就成了图形验证码了。
 代码如下 复制代码
function fun_code($sessionname)
{
 header("content-type:image/png");
 session_start();
 $authnum_session = '';
 //$str = 'abcdefghijkmnpqrstuvwxyz1234567890';
 $str = '1234567890';
 $l = strlen($str);
 for($i=1;$i<=4;$i++)
 {
  $num=rand(0,$l-1);
  $authnum_session.= $str[$num];
 }
 $_session[$sessionname]=$authnum_session;
 srand((double)microtime()*1000000);
 $im = imagecreate(50,20);
 $black = imagecolorallocate($im, 0,0,0);
 $white = imagecolorallocate($im, 255,255,255);
 $gray = imagecolorallocate($im, 200,200,200);
 imagefill($im,68,30,$gray);
 for($i=0;$i<3;$i++)
 {
  imageline($im,rand(0,30),rand(0,21),rand(20,40),rand(0,21),$li);
 }
 imagestring($im, 5, 8, 2, $authnum_session, $white);
 for($i=0;$i<90;$i++)
 {
  imagesetpixel($im, rand()%70 , rand()%30 , $gray);
 }
 imagepng($im);
 imagedestroy($im);
}
echo fun_code("code");
php gd库是不可以给gif图片加水印了,如果要利用php给gif图片增加水印的话,就得借助于一款ImageMagick功能的PHP扩展,它可以帮我们完成我们想要的功能哦。
 代码如下 复制代码
<?php
$imagedraw = new imagick();
$pixel = new imagickpixel('gray');
$pixel->setcolor('black');
$imagedraw->newimage(100, 75, $pixel);
$draw = new imagickdraw();
$draw->setfont('bookman-demiitalic');
$draw->setfontsize(12);
$image=new imagick();
$animation = new imagick();
$animation->setformat( "gif" );
$image->readimage("old.gif");
$unitl = $image->getimageindex();
$image->writeimages('animation.gif',false);
$delay = $image->getimagedelay();
$filename = 'animation-';
for ($i=0; $i<$unitl; $i++) {
    $thisimage = new imagick();
    $thisimage->readimage($filename.$i.'.gif');
    $thisimage->annotateimage($draw, 0, 12, 0, 'copyright by mpeg');
    $animation->addimage($thisimage);
    $animation->setimagedelay($delay);
}       
header("content-type: image/gif");
echo $animation->getimagesblob();
?>

imagemagick功能的php扩展。使用这个扩展可以使php具备和imagemagick相同的功能。   imagemagick是一套功能强大、稳定而且免费的工具集和开发包,可以用来读、写和处理超过185种基本格式的图片文件,包括流行的tiff, jpeg, gif, png, pdf以及photocd等格式。利用imagemagick,你可以根据web应用程序的需要动态生成图片, 还可以对一个(或一组)图片进行改变大小、旋转、锐化、减色或增加特效等操作,并将操作的结果以相同格式或其它格式保存。

组件下载地址

http://pecl.php.net/package/imagick
http://www.imagemagick.org

在php中要生成中文验证码就必须做与生成验证验证码不一样的操作,因为GD函数只接受UTF8格式编码的文字,所以在用php生成中文验证码时面要前首先要进行编码转换,操作php的iconv可以实例。
 代码如下 复制代码

$ch_str="你要生成中文验证码汉字";
$str=array();
for ($i=0;$i<strlen($ch_str);$i+=3)
{
    $str[]=$ch_str[$i].$ch_str[$i+1].$ch_str[$i+2];
}
//图片的长和高
$image_x=200;
$image_y=100;
$im = imagecreate($image_x,$image_y);
//这里取图片底色为白色
$bkg = imagecolorallocate($im,255,255,255);
//显示的字体样式,这个要把文件放到对应的目录中,如果你没有文件就去window的字体文件中找一个吧。
$fnt = "simfang.ttf";
//为图像分配一些颜色
$white=imagecolorallocate($im,234,185,95);
//在图片上画椭圆弧,指定下坐标点
imagearc($im, 150, 8, 20, 20, 75, 170, $white);
imagearc($im, 180, 7,50, 30, 75, 175, $white);
//在图片上画一条线段,指定下坐标点
imageline($im,20,20,180,30,$white);
imageline($im,20,18,170,50,$white);
imageline($im,25,50,80,50,$white);
//乱点的数量
$noise_num=3000;
$line_num=80;
//各种混乱字符的颜色
$rectangle_color=imagecolorallocate($im,0xaa,0xaa,0xaa);
$noise_color=imagecolorallocate($im,0x00,0x00,0x00);
$font_color=imagecolorallocate($im,0x00,0x00,0x00);
for($i=0;$i<$noise_num;$i++)
{
    //在一个坐标点上画一个单一像素,这个点上面定义了,是黑色的。
    imagesetpixel($im,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
}

for($i=0;$i<$line_num;$i++)
{
    $line_color=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    //在两个坐标点间画一条线,颜色在上面定义
    imageline($im,mt_rand(0,$image_x),mt_rand(0,$image_y),mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);   
}
$randnum=rand(0,count($str)-4);
//保持是偶数
if ($randnum%2)
{
    $randnum+=1;   
}
$str1=$str[$randnum].$str[$randnum+1];
for ($i=0;$i<2;$i++)
{
    imagettftext($im, rand(28,32), rand(0,70), rand(($image_x/4)*$i+$image_x/10,($image_x/4)*$i+$image_x/8), rand($image_y/2+$image_y/10,$image_y/2+$image_y/5), $font_color, $fnt, $str[$randnum+$i]);   
}
imagepng($im);
imagedestroy($im);

//生成中文验证码二
$str="中文汉字";
$image_x=110;
$image_y=110;
$im = imagecreate($image_x,$image_y);
$bkg = imagecolorallocate($im,255,255,255);
$fnt = "hb.ttf"; //显示的字体样式
$white=imagecolorallocate($im,234,185,95);
imagearc($im, 150, 8, 20, 20, 75, 170, $white);
imagearc($im, 180, 7,50, 30, 75, 175, $white);
imageline($im,20,20,180,30,$white);
imageline($im,20,18,170,50,$white);
imageline($im,25,50,80,50,$white);
$noise_num=3000;
$line_num=80;
imagecolorallocate($im,0xff,0xff,0xff);
$rectangle_color=imagecolorallocate($im,0xaa,0xaa,0xaa);
$noise_color=imagecolorallocate($im,0x00,0x00,0x00);
$font_color=imagecolorallocate($im,0x00,0x00,0x00);
$line_color=imagecolorallocate($im,0x00,0x00,0x00);
for($i=0;$i<$noise_num;$i++)
imagesetpixel($im,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);
for($i=0;$i<$line_num;$i++)
imageline($im,mt_rand(0,$image_x),mt_rand(0,$image_y),mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
$randnum=rand(0,strlen($str)-4);
if ($randnum%2)$randnum+=1;
$str1=substr($str,$randnum,4);
$str2 = iconv("gb2312","utf-8",$str1);//验证汉字在$str1里面

imagettftext($im, rand(28,32), rand(0,70), rand(25,27), rand(70,100), $font_color, $fnt, $str2);
imagepng($im);
imagedestroy($im);

//把汉字放在数组
/*
gd函数只接受utf8格式编码的文字,所以在写文字前首先要进行编码转换。php自带的iconv和mbstring库都可以完成这项工作
*/

$randcode=array('宠');
$codetable=array();
$fp=fopen("gb2312.txt","r");
while($line=fgets($fp))
    $codetable[hexdec(substr($line,0,6))]=substr($line,7,6);
fclose($fp); 

//gb2312转utf8
function gb2utf8($gbstr)
{
    global $codetable;
    if(trim($gbstr)=="")
        return $gbstr;
    $ret="";
    $utf8="";
    while($gbstr)
    {
        if(ord(substr($gbstr,0,1))>127)
        {
            $thisw=substr($gbstr,0,2);
            $gbstr=substr($gbstr,2,strlen($gbstr));
            $utf8="";
            @$utf8=u2utf8(hexdec($codetable[hexdec(bin2hex($thisw))-0x8080]));

 

            if($utf8!="")
            for($i=0;$i<strlen($utf8);$i+=3)
                $ret.=chr(substr($utf8,$i,3));
        }
        else
        {
            $ret.=substr($gbstr,0,1);
            $gbstr=substr($gbstr,1,strlen($gbstr));
        }
    }
    return $ret;

 

//unicode转utf8
function u2utf8($c)
{
    $str="";
    if($c<0x80)
        $str.=$c;
    elseif($c<0x800)
    {
        $str.=(0xc0|$c>>6);
        $str.=(0x80|$c&0x3f);
    }
    elseif($c<0x10000)
    {
        $str.=(0xe0|$c>>12);
        $str.=(0x80|$c>>6&0x3f);
        $str.=(0x80|$c&0x3f);
    }
    elseif($c<0x200000)
    {
        $str.=(0xf0|$c>>18);
        $str.=(0x80|$c>>12&0x3f);

 

        $str.=(0x80|$c>>6&0x3f);
        $str.=(0x80|$c&0x3f);
    }
    return $str;

//生成附加码
function create_excode($length)
{
 global $randcode;
    header("content-type: image/png");
 $image_x=$length*30;    //图片宽度
 $image_y=40;            //图片高度
    $noise_num=80*$length;   //杂点数量
    $line_num=$length-2;      //干扰线数量
 $image=imagecreate($image_x,$image_y);
 imagecolorallocate($image,0xff,0xff,0xff);                  //设定背景颜色
 $rectangle_color=imagecolorallocate($image,0xaa,0xaa,0xaa); //边框颜色
 $noise_color=imagecolorallocate($image,0x00,0x00,0x00);     //杂点颜色
 $font_color=imagecolorallocate($image,0x00,0x00,0x00);      //字体颜色
 $line_color=imagecolorallocate($image,0x33,0x33,0x33);      //干扰线颜色

 //加入杂点
    for($i=0;$i<$noise_num;$i++)
  imagesetpixel($image,mt_rand(0,$image_x),mt_rand(0,$image_y),$noise_color);

 $font_face="simkai.ttf";    //字体
    $x=2;
    $session_code='';
    for($i=0;$i<$length;$i++)
    {
        $code=$randcode[mt_rand(0,count($randcode)-1)];
     imagettftext($image,18,mt_rand(-6,6),$x,29,$font_color,$font_face,gb2utf8($code));
        $x+=30;
        $session_code.=$code;
    }
    @session_start();
    $_session['excode']=$session_code;  //把附加码的值放在session中

 
    //加入干扰线
    for($i=0;$i<$line_num;$i++)
     imageline($image,mt_rand(0,$image_x),mt_rand(0,$image_y),
                    mt_rand(0,$image_x),mt_rand(0,$image_y),$line_color);
 imagerectangle($image,0,0,$image_x-1,$image_y-1,$rectangle_color);  //加个边框
 imagepng($image);
 imagedestroy($image);
}
create_excode(6);

 

// 使用的时候直接用html语法:<img src="excode.php">调用就可以了,在服务端做验证时取session存储的验证字符与用户提交的字符进行比较,相同则通过验证

使用ImageCreate()创建一个代表空白图像的变量,这个函数要求以像素为单位的图像大小的参数,其格式是ImageCreate(x_size, y_size)。如果要创建一个大小为250×250的图像,就可以使用下面的语句:   

<? header ("content-type: image/png");  

   

 代码如下 复制代码
  $newimg = imagecreate(250,250);  

 

  由于图像还是空白的,因此你可能会希望用一些彩色来填充它。你需要首先使用imagecolorallocate()函数用其rgb值为这种颜色指定一个名字,这一函数的格式为imagecolorallocate([image], [red], [green], [blue])。如果要定义天蓝色,可以使用如下的语句:  

 

 代码如下 复制代码
  $skyblue = imagecolorallocate($newimg,136,193,255);  

 

  接下来,需要使用imagefill()函数用这种颜色填充这个图像,imagefill()函数有几个版本,例如imagefillrectangle()、imagefillpolygon()等。为简单起见,我们通过如下的格式使用imagefill()函数:  

 代码如下 复制代码

  imagefill([image], [start x point], [start y point], [color])

  imagefill($newimg,0,0,$skyblue);  

 

  最后,在图像建立后释放图像句柄和所占用的内存:  

 

 代码如下 复制代码

 imagepng($newimg);

  imagedestroy($newimg); ?>

  这样,创建图像的全部代码如下所示:

      php教程代码:
 

 代码如下 复制代码

<? header ("content-type: image/png");

  $newimg = imagecreate(250,250);

  $skyblue = imagecolorallocate($newimg,136,193,255);

  imagefill($newimg,0,0,$skyblue);

  imagepng($newimg);

  imagedestroy($newimg);

  ?>   

 代码如下 复制代码

<?php教程

/**
 *  ecshop jpgraph图表类库
 *  ==============================================================
 *  利用开源的jpgraph库实现对各类型数据以图表,走势图的形式表现出来。
 *  ==============================================================
**/

class cls_jpgraph
{
 var $db = null;
 var $ydata = array();
 var $width = 350;
 var $height = 250;

 var $graph = ''; //图形对象
 var $piegraph = ''; //丙状图形

 var $lineplot = ''; //线性对象
 var $barpot = ''; //柱状对象
 var $gbplat = ''; //柱状组对象
 var $txt = '';  //文本对象
 var $p1 = '';  //丙状图对象

 var $scale = ''; //图表类型? (textlin,textlog,intlin)
 var $yscale = '';   // (log,)
 var $xgrid = false; //x轴网格显示
 var $ygrid = false; //y轴网格显示
 var $title = ''; //标题
 var $subtitle = ''; //子标题(一般为日期)
 var $xaxis = ''; //x轴名称
 var $yaxis = ''; //y轴名称

 /*margin position*/
 var $left = 0;
 var $right = 0;
 var $top = 0;
 var $bottom = 0;

 /**
 *构造函数
 */
 function cls_jpgraph($width=350,$height=250)
 {
  $this->width = $width;
  $this->height = $height;

  $this->graph = new graph($this->width,$this->height);

 }

 /**
 * 图表类库的构造函数
 *
 */
 /*
 function __construct($parms,$width,$height)
 {
  cls_jpgraph($parms,$width,$height);
 }
 */

 /*图片基本信息设置*/
 function set_jpgraph($scale='intlin',$title='',$subtitle='',$bgcolor='',$xaxis='',
               $yaxis='',$xgrid=false,$ygrid=false,$margin='') {

  $this->scale = $scale;
  $this->title = $title;
  $this->subtitle = $subtitle;
  $this->xaxis = $xaxis;
  $this->yaxis = $yaxis;
  $this->xgrid = $xgrid;
  $this->ygrid = $ygrid;

  if(!empty($scale)) {
   $this->graph->setscale($this->scale);
  }
  else {
  $this->graph->setscale('intlin');
  }
  $this->graph->xgrid->show($this->xgrid,$this->xgrid);
  $this->graph->ygrid->show($this->ygrid,$this->ygrid);

  if(!empty($bgcolor)) {
   $this->graph->setmargincolor($bgcolor);
  }

  /*如果手工设置了图片位置*/
  if(is_array($margin)) {
   while(list($key,$val) = each($margin)) {
    $this->$key = $val;
   }
   $this->graph->setmargin($this->left,$this->right,$this->top,$this->bottom);
  }

  if(!empty($this->title)) {
   $this->graph->title->set($this->title);
  }
  if(!empty($this->subtitle)) {
   $this->graph->subtitle->set($this->subtitle);
  }
  else {
   $this->graph->subtitle->set('('.date('y-m-d').')'); //默认子标题设置为当前日期
  }
  if(!empty($this->xaxis)) {
   $this->graph->xaxis->title->set($this->xaxis);
  }
  if(!empty($this->yaxis)) {
   $this->graph->yaxis->title->set($this->yaxis);
  }

 }

 /*创建线性关系图表(linear plot)*/
 function create_lineplot($parms,$color='black',$weight=1)
 {
  $this->ydata = $parms;
  $this->lineplot = new lineplot($this->ydata);
  $this->lineplot->setcolor($color);
  $this->lineplot->setweight($weight);

  return $this->lineplot;
 }

 /*创建柱状图表(bar pot)*/
 function create_barpot($parms,$color='black',$width='0')
 {
  $this->ydata = $parms;
  $this->barpot = new barplot($this->ydata);
  $this->barpot->setfillcolor($color);
  if(!empty($width)) {
   $this->barpot->setwidth($width);
  }

  return $this->barpot;
 }

 /*创建数据柱状图表组*/
 function create_bargroup($plotarr,$width='0.8')
 {
  $this->gbplot = new groupbarplot($plotarr);
  $this->gbplot->setwidth($width);

  return $this->gbplot;
 }

 /*创建文本内容*/
 function create_text($str,$postion='',$color='black')
 {
  $this->txt = new text($str);

  if(is_array($postion)) {
   while(list($key,$val) = each($postion)) {
    $this->$key = $val;
   }
   $this->txt->setpos($this->left,$this->top);
  }
  else {
   $this->txt->setpos(10,20);
  }
  $this->txt->setcolor($color);

  $this->graph->add($this->txt);
 }

 /*创建丙状图表*/
 function create_pie($parms,$title,$type='3d',$size='0.5',$center='0.5',$width='350',$height='250')
 {
  $this->width = $width;
  $this->height = $height;

  $this->piegraph = new piegraph(300,200);
  $this->piegraph->setshadow();

  $this->piegraph->title->set($title);

  if('3d' != $type) {
   $this->p1 = new pieplot($parms);
   $this->piegraph->add($this->p1);
  }
  else {
   $this->p1 = new pieplot3d($parms);
   $this->p1->setsize($size);
   $this->p1->setcenter($center);
//   $this->p1->setlegends($gdatelocale->getshortmonth());
   $this->piegraph->add($this->p1);
  }
  $this->piegraph->stroke();
 }

 function get_auth_code($length=4)
 {
  $spam = new antispam();
  $chars = $spam->rand($length);

  if( $spam->stroke() === false ) {
   return false;
  }

  return $chars;
 }

 /*完成图形创建并显示*/
 function display($obj)
 {
  $this->graph->add($obj);
  $this->graph->stroke();
 }
}

?>

[!--infotagslink--]

相关文章

  • 金额阿拉伯数字转换为中文的存储过程

    Create Procedure AtoC @ChangeMoney Money as Set Nocount ON Declare @String1 char(20) Declare @String2 char(30) ...2016-11-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • c#中判断字符串是不是数字或字母的方法

    这篇文章介绍了C#判断字符串是否数字或字母的实例,有需要的朋友可以参考一下...2020-06-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 微信小程序 页面传值详解

    这篇文章主要介绍了微信小程序 页面传值详解的相关资料,需要的朋友可以参考下...2017-03-13
  • C#使用Process类调用外部exe程序

    本文通过两个示例讲解了一下Process类调用外部应用程序的基本用法,并简单讲解了StartInfo属性,有需要的朋友可以参考一下。...2020-06-25
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • PHP 验证码不显示只有一个小红叉的解决方法

    最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
  • 使用GruntJS构建Web程序之构建篇

    大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
  • uniapp微信小程序:key失效的解决方法

    这篇文章主要介绍了uniapp微信小程序:key失效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-20
  • 微信小程序二维码生成工具 weapp-qrcode详解

    这篇文章主要介绍了微信小程序 二维码生成工具 weapp-qrcode详解,教大家如何在项目中引入weapp-qrcode.js文件,通过实例代码给大家介绍的非常详细,需要的朋友可以参考下...2021-10-23
  • jQuery Real Person验证码插件防止表单自动提交

    本文介绍的jQuery插件有点特殊,防自动提交表单的验证工具,就是我们经常用到的验证码工具,先给大家看看效果。效果图如下: 使用说明 需要使用jQuery库文件和Real Person库文件 同时需要自定义验证码显示的CSS样式 使用实例...2015-11-08
  • C#实现猜数字游戏

    这篇文章主要为大家详细介绍了C#实现猜数字游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • JS实现随机生成验证码

    这篇文章主要为大家详细介绍了JS实现随机生成验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-06
  • 将c#编写的程序打包成应用程序的实现步骤分享(安装,卸载) 图文

    时常会写用c#一些程序,但如何将他们和photoshop一样的大型软件打成一个压缩包,以便于发布....2020-06-25
  • 安卓开发之设置密码只能输入字母和数字的组合

    设置登录密码我们一般会有限制的如由什么组合了,下面我们来看一篇关于安卓开发之设置密码只能输入字母和数字的组合方法,具体的细节如下所示。 无论是电脑还是手机...2016-09-20
  • PHP常用的小程序代码段

    本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:1.计算两个时间的相差几天$startdate=strtotime("2009-12-09");$enddate=strtotime("2009-12-05");上面的php时间日期函数strtotime已经把字符串...2015-11-24
  • 微信小程序 网络请求(GET请求)详解

    这篇文章主要介绍了微信小程序 网络请求(GET请求)详解的相关资料,需要的朋友可以参考下...2016-11-22
  • 微信小程序自定义tabbar组件

    这篇文章主要为大家详细介绍了微信小程序自定义tabbar组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-14
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10