php+ajax进度条实时显示文件处理进度

结合网上高手的图片色调检查程序,做了这个小东西。可以将文件夹中颜色大面积接近人体肤色的图片检查出来。

这是我学习中做的,也是发布的第一个程序,肯定有很多问题,大家不要丢砖啊

先上几张截图

 

半天没找到哪里发附件,源码没发上了,有

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>图片检查</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script type="text/javascript" src="ajax_min.js"></script>
<style>
body{font-size:13px;}
.a{color:#666}
.red{color:#F00}
#status{ width:590px; height:30px; overflow:hidden; line-height:30px; padding-left:10px; margin:5px 0px; border:1px #333 solid; background-color:#F7F7F7; color:#000; text-align:left}
.info { height:25px;font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; text-align:left}
#now{width:1%; height:20px;background-color:#099; text-align:left}
#time{position:absolute; z-index:400; margin-top:-20px;width:600px; height:20px; line-height:20px; color:#C30; border:0px; text-align:center}
#info{width:600px; height:20px; line-height:20px; background-color:#FFF; color:#C30; border:1px #666 solid; margin-bottom:5px; text-align:left}
#scrolldIV{overflow-y:auto;overflow-x:hidden; height: 200px; width: 590px; border: 1px solid #999; text-align:left; padding:5px 5px 30px 5px}
</style>
<script type="text/javascript">
function $(id){
return 	document.getElementById(id);
}
function result(text){
var msg=text.split("|");
$("status").innerHTML=msg[0];	
$("scrolldIV").innerHTML+=msg[1];
$('scrolldIV').scrollTop =$('scrolldIV').scrollHeight;
}
function callBack_result(text){//回调函数
//alert(text);
var msg=text.split("|");
var txt=msg[1].split(",");
  if (txt[0]==1){
    $("scrolldIV").innerHTML+=txt[1];
   }
if (msg[0]==0){//
$("show").style.display='none';
$("scrolldIV").style.display='';
$("scrolldIV").innerHTML=msg[1];
}else{
$("status").innerHTML="("+msg[4]+"/"+msg[3]+") "+msg[6];
var n=msg[7];
var div = $('scrolldIV');
$('time').innerHTML=(n*100).toFixed(0)+'%';
$('time').style.color=(n*100).toFixed(1)>=50?'#FFF':'#C30';
$('now').style.width=(n*100)+'%';
div.scrollTop = div.scrollHeight;
if (msg[8]==0){	
ajax.get("check.php?act=ok&do=c&f="+msg[2]+"&total="+msg[3]+"&now="+msg[4]+"&err="+msg[5]+"&now_f="+msg[6],callBack_result);
}else if(msg[8]==1) {
$("status").innerHTML='正在进行扫尾工作……';
ajax.get("check.php?act=ok&do=d&f="+msg[2]+"&err="+msg[5],result);
}
}
}
function check(){
$("show").style.display='';
$("scrolldIV").style.display='';
$("scrolldIV").innerHTML='';
$("status").innerHTML='数据初始化中,请稍后……';
$('time').innerHTML='0%';
$('time').style.color='#C30';
$('now').style.width='0%';
var mulu=$("mulu").value;
ajax.get("check.php?act=ok&mulu="+mulu+"&Rnd="+ new Date().getTime(),callBack_result);
}  
</script>

</head>
<body>
<div style="margin:30px auto" align="center">
<form id="form1" name="form1" method="post" action="?act=mulu">
 <p> 输入目录:
  <input name="mulu" type="text" id="mulu" size="50" />
  <input type="button" name="button" id="button" value="搜索" onclick="check();" /> 
  (默认为当前目录)</p>
</form>
<div id="show" style="display:none">
<div id="status"></div>
<div id="info">
   <div id="now"></div><div id="time"></div></div>
</div>
<div id="scrolldIV" style="display:none"></div>
</div>
</body>
</html>

<?php
header("content-Type: text/html; charset=gb2312");
error_reporting(0);
ini_set('display_errors',0) ;
set_time_limit(0);
date_default_timezone_set("PRC");
$infotime=date('YmdHis');

//函数开始————————
//列出所有文件
function get_all_files($path){
    $list = array();
    foreach( glob( $path . '/*') as $item ){
        if( is_dir( $item ) ){
         $list = array_merge( $list , get_all_files( $item ) );
        }
        else{
         if(eregi(".jpg",$item)||eregi(".gif",$item)){
			 $list[] = $item;
			 }//这里可以增加判断文件名或其他。changed by:edlongren
         //$list[] = $item;
        }
    }
    return $list;
}

function openImageFile($file_name){   
    list($width, $height, $type, $attr) = getimagesize($file_name);   
    switch ($type){   
	        case 1 :   
            $ext = "gif";   
            break;   
        case 2 :   
            $ext = "jpeg";   
            break;   
        case 3 :   
            $ext = "png";   
            break;   
    }
	$image_handle='imagecreatefrom'.$ext;
    $image_handle=$image_handle($file_name); 
    return array('image_handle'=>$image_handle,'width'=>$width,'height'=>$height);   
}   
function testSkin($image_handle,$width,$height){   
    $skin_pix = 0;   
    for($w=0;$w<$width;$w++){   
        for ($h=0;$h<$height;$h++){   
            //验证图片   
            $rgb = imagecolorat($image_handle,$w,$h);   
            $r = ($rgb >> 16) & 0xFF;   
            $g = ($rgb >> 8) & 0xFF;   
            $b = $rgb & 0xFF;   
            $Y=0.299*$r+0.587*$g+0.114*$b;   
            $Cb=0.564*($b-$Y)+128;   
            $Cr=0.713*($r-$Y)+128;   
            if($Cb >= 86 && $Cb <= 117 && $Cr >= 140 && $Cr <= 168){   
                $skin_pix ++;   
            }   
        }   
    }   
    $skin_rate =  $skin_pix/($width*$height);   
    return $skin_rate;   
}
//函数结束————————
	//echo $list;
	//echo $path;
if (isset($_GET['act'])){
	$do=isset($_GET['do'])?$_GET['do']:'f';
	$path=!empty($_GET['mulu'])?trim($_GET['mulu']):'.';
	$list=!empty($_GET['list'])?nl2br($_GET['list']):'';
if(($path||!empty($list))&&$do=='f'){   //读取要检查的对象
	if (!empty($list)){
	$img_list=explode('<br />',$list);//统计页面总数
	if (count($img_list)==0) $img_list[]=$list;
	}else{
$img_list=get_all_files($path);
	}
	$img_list=array_unique($img_list);
$count=count($img_list);
if ($count>0){
	//写入检查文件列表
$f_w="array(";
	foreach ($img_list as $e_img){
	$f_w.="'".$e_img."', \n";	
	}
$f_w=rtrim($f_w,",").")";
	$user_now_list="<?php \n";
	$user_now_list.="\$f_list=".$f_w." \n";
    $user_now_list.="?>";
		$fp = @fopen('temp/'.$infotime.'_temp.php', "w"); 
fwrite($fp,$user_now_list); 
fclose($fp);

die ("1|1,当前搜索目录为: <font class=red>".$path."</font><br/>共搜索到 <font class=red>".$count."</font> 张图片,开始检测……<br/>|".$infotime."|".$count."|0|0|初始化完毕!|0|0");
	}else{
die ("0|<p>当前搜索目录为: <font class=red>".$path."</font></p><p>共搜索到 <font class=red>0</font> 张图片</p>");
	}
}
  if ($do=='c'){//检查图片
	  $f_name=$_GET['f'];
	 $f_total=intval($_GET['total']);
	 $f_now=intval($_GET['now']);
     $err=intval($_GET['err']);
include "temp/{$f_name}_temp.php";
	$file_name =$f_list[$f_now];
	$f_now+=1;
	$go=($f_total==$f_now)?1:0;
    if(is_file($file_name)){
        //文件存在  
        $image = openImageFile($file_name);   
        $rate = testSkin($image['image_handle'],$image['width'],$image['height']);   
        if($rate > 0.3){
			$err+=1;
die("1|1,<font class=red>警告:<a href=\"$file_name\" target=\"_blank\">".$file_name."</a> 特征明显!肤色比例:".$rate." </font><br/>|".$f_name."|".$f_total."|".$f_now."|".$err."|正在扫描:".$file_name."|".($f_now/$f_total)."|".$go);		
        }else {   
die("1|0,0|".$f_name."|".$f_total."|".$f_now."|".$err."|正在扫描:".$file_name."|".($f_now/$f_total)."|".$go);
        }   
    }else {   
die("1|1,系统找不到 ".$file_name." 文件! <br/>|".$f_name."|".$f_total."|".$f_now."|".$err."|正在扫描:".$file_name."|".($f_now/$f_total)."|".$go);
    }
  }
if ($do=='d'){//扫尾工作,删除临时文件
	$f_name=$_GET['f'];
	 $err=intval($_GET['err']);
	if (unlink("temp/{$f_name}_temp.php")){
	die("扫描完成!|<p>共找到 <font class=red>".$err."</font> 个可疑文件</p>");
	}else{
		die("扫描完成!|共找到 <font class=red>".$err."</font> 个可疑文件<br/><p>临时文件<font class=red> ".$f_name."</font> 删除失败,请手动删除!</p>");	
	}
  }
  
}
?> 

posted @ 2010-11-23 13:36  剑客落落  阅读(2007)  评论(0编辑  收藏  举报