php实现验证码

首先确保安装php的GD库
然后利用php的绘图函数可以绘制验证码
下面是一个完整实现验证码的php文件:
header('Content-type: image/png'); //show the file is a image file and the format is png
$char_base = array(3,4,5,6,7,8,9,a,b,c,d,e,f,h,k,m,n,r,s,t,u,v,w,x,y,A,C,E,F,G,H,K,M,N,Q,S,T,U,V,W,X,Y);
for($num=1;$num<=6;$num++){
    $rand_num = rand(0,41);
    $str_key .= $char_base[$rand_num];
    } //get the random string
$im = imagecreate(95,32); // get the base picture
$gray = ImageColorAllocate($im, 200,200,200);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255); //get the three color
putenv('GDFONTPATH='.realpath('.')); //set the font file path
$font = "ariali.ttf"; // set the font file name
$fontsize = "20";
$fontangle = "0";   
imagefill($im,92,30,$gray);
for($i=0;$i<6;$i++){
   $angle_rand = rand(-20,20);
   $size_rand = rand(15,18);
   $y_rand = rand(20,30);
   $color_rand = rand(20,200);
   $randcolor = ImageColorallocate($im,rand(0,100),rand(0,100),rand(0,100));
   imagettftext($im, $size_rand , $angle_rand , $i*15+2, $y_rand, $randcolor , $font, substr($str_key,$i,1));
}  //draw the string on the base picture
 for($i=0;$i<200;$i++)
 {
 $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
 imagesetpixel($im, rand()%95 , rand()%30 , $randcolor);
 }  // draw the background noise
 ImagePNG($im); //output
 ImageDestroy($im); // release the source
改边字体文件,可以改变输出字符窜的风格
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值