<?
session_start(); 
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/admin.css" rel="stylesheet" type="text/css">
<title>无标题文档</title></head>
<?
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	//require_once('inc/public.php'); //main menu & left menu
	$opendb=new mydb();
	require_once('inc/char_set.php');
	

	$submit=$_POST['submit'];
	$wtString=$_POST['wtString'];
	$wtFont=$_POST['wtFont'];
	$wtSize=$_POST['wtSize'];
	$wtColor=$_POST['wtColor'];
	$wtPosition=$_POST['wtPosition'];
	$allPic=$_POST['allPic'];
	$wtType=$_POST['wtType'];
	$picFile=$_FILES['picFile']['tmp_name'];
	$wPic="";// watermarkfile in ls-admin/images
	$clearallWatermark=$_POST['clearallWatermark'];


	$waterMarkFile="";

		
	$cate_sql="select * from ls_products where midcateID is null and midcateName is null and subcateName is null and subcateID is null and productName is null and productID is null ";
	$cate_query=$opendb->query($cate_sql);
	while($cate_rows=mysql_fetch_assoc($cate_query))
	{
		$cateid=$cate_rows['ID'];
			//大类
			$sqlstr1="select * from ls_products where midcateID =".$cateid." ";
			$query1=$opendb->query($sqlstr1);
			$mynum1=mysql_num_rows($query1);
			
			if($mynum1!=0)//有中类
			{
				while($myrows1=mysql_fetch_assoc($query1))
				{
					$sqlstr2="select * from ls_products where subcateID =".$myrows1['ID']." ";
					$query2=$opendb->query($sqlstr2);
					$mynum2=mysql_num_rows($query2);
					if($mynum2!=0) //有小类
					{
						while($myrows2=mysql_fetch_assoc($query2))
						{
							$folder=$cate_rows['cateFolders']."/".$myrows1['midcateFolders']."/".$myrows2['subcateFolders']."/".$BigFoldersProducts;
							//echo $folder;
							eachPic($folder,$allPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$picFile);
							if($clearallWatermark==1)		//clear all watermark
							{clearEachPic($folder);}

							if($waterMarkThumbnails)
							{
								$folder=$cate_rows['cateFolders']."/".$myrows1['midcateFolders']."/".$myrows2['subcateFolders']."/".$SmallFoldersProducts;
								eachPic($folder,$allPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$picFile);
								if($clearallWatermark==1)		//clear all watermark
								{clearEachPic($folder);}

							}

						}
					}
					else //没有小类
					{							

						$folder=$cate_rows['cateFolders']."/".$myrows1['midcateFolders']."/".$BigFoldersProducts;
						//echo $folder;
						eachPic($folder,$allPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$picFile);
						if($clearallWatermark==1)		//clear all watermark
						{clearEachPic($folder);}

						if($waterMarkThumbnails)
						{
							$folder=$cate_rows['cateFolders']."/".$myrows1['midcateFolders']."/".$SmallFoldersProducts;
							eachPic($folder,$allPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$picFile);
							if($clearallWatermark==1)		//clear all watermark
							{clearEachPic($folder);}
						}

					}
				}

			}
			else
			{
				$folder=$cate_rows['cateFolders']."/".$BigFoldersProducts;
				eachPic($folder,$allPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$picFile);
				if($clearallWatermark==1)		//clear all watermark
				{clearEachPic($folder);}

				if($waterMarkThumbnails)
				{
					$folder=$cate_rows['cateFolders']."/".$SmallFoldersProducts;
					eachPic($folder,$allPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$picFile);
					if($clearallWatermark==1)
					{clearEachPic($folder);}
				}
			}
	}
	
	//set the watermark flag and watermark flag 
	//$cate_wt="update ls_products set waterMark=1  where  productPic is not null ";
	//$cate_query=$opendb->query($cate_wt);

	if($submit=="生成水印")
	{
		if($wtType=="pic")
		{
		$waterMarkFile=$wPic; //get global var from addWatermark
		$modi_sql="update ls_watermark set wtImage='".$waterMarkFile."', wtFont='".$wtFont."', wtSize='".$wtSize."', wtColor='".$wtColor."', wtPosition='".$wtPosition."',date='".date('Y-m-d')."' where ID=1  ";
		}else{
		$modi_sql="update ls_watermark set wtString='".$wtString."', wtFont='".$wtFont."', wtSize='".$wtSize."', wtColor='".$wtColor."', wtPosition='".$wtPosition."',date='".date('Y-m-d')."' where ID=1  ";
		}
			//echo $modi_sql;
		$modi_query=$opendb->query($modi_sql);
	}
	
	echo "<br/><br/><a href=\"watermarkstr.php\"><u>返回</u></a><br/><br/>";
	$opendb->close();

/*


*/
function eachPic($folder,$allPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$picFile)
{
	global $waterMarkFile;
	$opendb=new mydb();
	require_once('inc/char_set.php');
	
	$picFolder="../".$folder;
	if(file_exists($picFolder))
	{
	if ($handle = opendir($picFolder)) 
	{

	while (false !== ($file = readdir($handle))) 
	{
		if ($file != "." && $file != "..")
		{
			//echo "--$file\n";
			if($allPic==0)
			{
				$sql_wt="select * from ls_products where productPic='".$file."' ";
				$query_wt=$opendb->query($sql_wt);
				$rows_wt=mysql_fetch_assoc($query_wt);
				if($rows_wt['waterMark']==1||$rows_wt['waterMark']==null)
				{continue;}
			}
			$pathfile=$picFolder."/".$file;
			//echo $pathfile.$i."**";
			$oriPic=$pathfile;
			$bakPic=$pathfile.'.bak';

			list($width, $height, $type, $attr) = getimagesize($oriPic);
  			$size=filesize($oriPic); 
			if($type==1||$type==2||$type==3)	//type is .gif,.jpg,.png
			{
			//echo substr($pathfile,strrchr($pathfile, '.'),4);
				if(strrchr($oriPic, '.')!=".bak")
				{
				if(!file_exists($bakPic))
				{
				
				if(!copy($oriPic, $bakPic) )
				{echo $oriPic." 备份失败！";}
				}
				//echo $oriPic;
				$srcPic=$bakPic;
				$desPic=$oriPic;
				//$watermark=createWatermark($str,$color,$font,$size);		//字符串生成水印图片
				echo $srcPic,$desPic,$wtString.$wtType.$picFile;
				if(addWatermark($srcPic,$desPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$picFile))
				{
					$updatesql="update ls_products set waterMark=1 where productPic='{$file}' ";
					$opendb->query($updatesql);
					echo " 水印已生成 ： ".$desPic."<br/>";
				}else{
					echo " 水印生成失败 ： ".$desPic."<br/>";
				}
				}

			}
		}
	
	}//end while
	closedir($handle);
	}
	else
	{
		echo "	<script>alert(' 目录不存在 ！');history.go(-1);</script>";
	}
	}
}

function clearEachPic($folder)
{
	$opendb=new mydb();
	require_once('inc/char_set.php');
	
	$picFolder="../".$folder;
	if(file_exists($picFolder))
	{
		if ($handle = opendir($picFolder)) 
		{

   		 while (false !== ($file = readdir($handle))) 
		{
			if ($file != "." && $file != "..")
			{
			//echo "--$file\n";
				$pathfile=$picFolder."/".$file;
				//echo $pathfile.$i."**";
				$oriPic=$pathfile;
				$bakPic=$pathfile.'.bak';
				if(strrchr($oriPic, '.')!=".bak")
				{
					if(copy($bakPic,$oriPic ) )
					{echo " 水印已清除 ： ".$oriPic."<br/>";}
					else
					{echo " 水印清除失败 ： ".$oriPic."<br/>";}
					}
				}

			}
	
		}//end while
		closedir($handle);
	}
	else
	{
		echo "	<script>alert(' 目录不存在 ！');history.go(-1);</script>";
	}
}


?>
