首先确保安装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
改边字体文件,可以改变输出字符窜的风格
然后利用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
改边字体文件,可以改变输出字符窜的风格