首页 > php学习 > php生成文件夹版验证码

php生成文件夹版验证码

2015年7月14日 发表评论 阅读评论

1,文件夹yzm.png

2,文件:confirmcode.class.php

3,文件:create.php

4,字体:font.ttf

5,文件:如何调用.php

——————————-

confirmcode.class.php:

<?php
header(‘Content-type:image/png’);
class imageCaptcha{
private $height;
private $width;
private $textNum;
private $textContent;
private $fontColor;
private $randFontColor;
private $fontSize;
private $fontFamily;
private $bgColor;
private $randBgColor;
private $textLang;
private $noisePoint;
private $noiseLine;
private $distortion;
private $distortionImage;
private $showBorder;
private $image;
public function imageCaptcha(){
$this->textNum=4;
$this->fontSize=15;
$this->fontFamily=”;
$this->textLang=’en’;
$this->noisePoint=0;
$this->noiseLine=0;
$this->distortion=false;
$this->showBorder=false;
}
public function set_show_mode($w,$h,$num,$fc,$fz,$ff_url,$lang,$bc,$m,$n,$b,$border){
$this->width=$w;
$this->height=$h;
$this->textNum=$num;
$this->fontColor=sscanf($fc,’#%2x%2x%2x’);
$this->fontSize=$fz;
$this->fontFamily=$ff_url;
$this->textLang=$lang;
$this->bgColor=sscanf($bc,’#%2x%2x%2x’);
$this->noisePoint=$m;
$this->noiseLine=$n;
$this->distortion=$b;
$this->showBorder=$border;
}
public function initImage(){
if(empty($this->width)){
$this->width=floor($this->fontSize*1.3)*$this->textNum+10;
}
if(empty($this->height)){
$this->height=floor($this->fontSize*2.5);
}
$this->image=imagecreatetruecolor($this->width,$this->height);
if(empty($this->bgColor)){
$this->randBgColor=imagecolorallocate($this->image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
}
else{
$this->randBgColor=imagecolorallocate($this->image,$this->bgColor[0],$this->bgColor[1],$this->bgColor[2]);
}
imagefill($this->image,0,0,$this->randBgColor);
}
public function randText($type){
$string=”;
switch($type){
case ‘en’:
$str=’ABCDEFGHJKLMNPQRSTVWXYabcdehkmnprsvwxy35678′;
for($i=0;$i<$this->textNum;$i++){
$string=$string.’,’.$str[mt_rand(0,42)];
}
break;
}
return substr($string,1);
}
public function createText(){
$text_array=explode(‘,’,$this->randText($this->textLang));
$this->textContent=join(”,$text_array);
if(empty($this->fontColor)){
$this->randFontColor=imagecolorallocate($this->image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
}
else{
$this->randFontColor=imagecolorallocate($this->image,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
}
for($i=0;$i<$this->textNum;$i++){
$angle=mt_rand(-1,1)*mt_rand(1,20);
imagettftext($this->image,$this->fontSize,$angle,8+$i*floor($this->fontSize*1.3),3+floor($this->height*0.75),$this->randFontColor,$this->fontFamily,$text_array[$i]);
}
}
public function createNoisePoint(){
for($i=0;$i<$this->noisePoint;$i++){
$pointColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),$pointColor);
}
}
public function createNoiseLine(){
for($i=0;$i<$this->noiseLine;$i++){
$lineColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),20);
imageline($this->image,0,mt_rand(0,$this->width),$this->width,mt_rand(0,$this->height),$lineColor);
}
}
public function distortionText(){
$this->distortionImage=imagecreatetruecolor($this->width,$this->height);
imagefill($this->distortionImage,0,0,$this->randBgColor);
for($x=0;$x<$this->width;$x++){
for($y=0;$y<$this->height;$y++){
$rgbColor=imagecolorat($this->image,$x,$y);
imagesetpixel($this->distortionImage,(int)($x+sin($y/$this->height*2*M_PI-M_PI*0.5)*3),$y,$rgbColor);
}
}
$this->image=$this->distortionImage;
}
public function createImage(){
$this->initImage();
$this->createText();
$this->createNoisePoint();
$this->createNoiseLine();
if($this->distortion!=false){
$this->distortionText();
}
if($this->showBorder){
imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$this->randFontColor);
}
imagepng($this->image);
imagedestroy($this->image);
if($this->distortion!=false){
imagedestroy($this->distortionImage);
}
return $this->textContent;
}
}
$code_obj=new imageCaptcha();
$code_obj->set_show_mode(’85’,’30’,’4′,”,’15’,’font.ttf’,’en’,’#fcfefb’,mt_rand(5,10),mt_rand(1,3),false,false);
$code=$code_obj->createImage();
$f=fopen(‘code.txt’,”w”);
fwrite($f,strtolower($code));
fclose($f);
?>

——————————–

create.php:

<?php
//die(“closed”);
header(‘content-type:text/plain; charset=utf-8’);
set_time_limit(0);
for($i=1;$i<=20;$i++){
mkdir(‘yzm.png/’.$i);
for($k=1;$k<=50;$k++){
//获取图片
ob_start();
readfile(‘http://test.shiwufenzhong.com/create_yzm/confirmcode.class.php’);
$img=ob_get_contents();
ob_end_clean();
//获取验证码
$fp=fopen(‘code.txt’,”r”);
$code=fread($fp,1024);
fclose($fp);
//保存图片
$md5_code=hash(‘md5’,$code);
$md5_picname=substr(hash(‘md5’,$code.$i.$k),8,16);
$fp=fopen(‘yzm.png/’.$i.’/’.$md5_picname.’.png’,”w”);
fwrite($fp,$img);
fclose($fp);
//生成php文件夹
mkdir(‘yzm.png/’.$i.’/php’);
//生成php文件
$fp=fopen(‘yzm.png/’.$i.’/php/’.$k.’.php’,”w”);
fwrite($fp,”<?php \$yzm=array(‘pic’=>'”.$md5_picname.”‘,’key’=>'”.$md5_code.”‘);?>”);
fclose($fp);
}
}
die(“<h1>done …</h1>”);
?>

————————–

如何调用.php:

<?php
$yzmpngPath=mt_rand(1,20).’/’;
require(‘yzm.png/’.$yzmpngPath.’php/’.mt_rand(1,50).’.php’);
$session_id=$_POST[‘sessionid’];
if($session_id){
if(!preg_match(‘/^[a-zA-Z0-9]{26}$/’,$session_id)){exit();}
session_id($session_id);session_start();
}
else {session_start();$session_id=session_id();}
//请求超过3次的时候,以后每次请求间隔需要10秒
$new_session_n=intval($_SESSION[‘vanpeopleYZM’][‘requestNums’])+1;
if($new_session_n>4){
$time_line=$_SERVER[‘REQUEST_TIME’]-intval($_SESSION[‘vanpeopleYZM’][‘requestTime’]);
if($time_line<10){
echo json_encode(array(‘status’=>’error’,’errorStr’=>’速度过快,’.(10-$time_line).’秒后重试’,’fastCounterTime’=>10-$time_line,’sessionid’=>$session_id));
exit();
}
else {$new_session_n=1;}
}
$_SESSION[‘YZM’]=array(
‘key’=>$yzm[‘key’],’requestTime’=>$_SERVER[‘REQUEST_TIME’],’requestNums’=>$new_session_n
);
echo json_encode(
array(
‘status’=>’ok’,’sessionid’=>$session_id,’pic’=>$yzmpngPath.$yzm[‘pic’],’key’=>$yzm[‘key’]
)
);exit();
?>

 

分类: php学习 标签:
  1. 本文目前尚无任何评论.
您必须在 登录 后才能发布评论.
css.php