<?
/*13-3-1更新
//ls_watermark  至少有一条记录吗，且其ID=1
$GLOBALS['wPic']   constant文件 $waterMarkPic定义的 路径，在ls_syscode addWatermark函数加上了扩展名
*/
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');
	
	$wt_sql="select * from ls_watermark where ID= 1";
	$wt_query=$opendb->query($wt_sql);
	$wt_rows=mysql_fetch_assoc($wt_query);

	$submit=$_POST['submit'];
	$wtCount=0; // 添加水印计数器
	$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'];
	
	include "widget/watermark_createwtpic.php";
	
	$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);
	echo "<br/><br/><a href=\"watermarkstr.php\"><u>返回</u></a><br/><br/>";

	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;
							
							if($clearallWatermark==1)		//clear all watermark
							{clearEachPic($folder);}
							else
							{eachPic($folder,$allPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$picFile);}

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

						}
					}
					else //没有小类
					{							

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

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

					}
				}

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

				$folder=$cate_rows['cateFolders']."/".$SmallFoldersProducts; // 小图生成水印
				if($clearallWatermark==1)
				{clearEachPic($folder);}
				else
				{
					if($waterMarkThumbnails && file_exists($smallWTPic)) 
					{
						eachPic($folder,$allPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$smallWTPic);
					}
				}
			}
	}
	
	if($clearallWatermark==1)
	{
		$clearsql="update ls_products set waterMark =0";  //清除水印标志
		$clear_query=$opendb->query($clearsql);
		$clearsql ="update ls_watermark set wtString='', wtFont='', wtSize='', wtColor='', wtPosition='',date='".date('Y-m-d H:i:s')."', wtOperate='清除', wtTestfile='' where ID=1  ";   //清除水印记录
		$clear_query=$opendb->query($clearsql);
	}
	//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=="生成水印" && $wtCount!=0)  //没有计数没有生成
	{
		if($wtType=="pic" )
		{
			
			$modi_sql="update ls_watermark set wtImage='".$waterMarkFile."', wtFont='".$wtFont."', wtSize='".$wtSize."', wtColor='".$wtColor."', wtPosition='".$wtPosition."',date='".date('Y-m-d H:i:s')."' , wtOperate='生成' 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 H:i:s')."' , wtOperate='生成' where ID=1  ";
		}
			//echo $modi_sql;
		$modi_query=$opendb->query($modi_sql);
	}
	
	echo "<br/><br/><a href=\"watermarkstr.php\"><u>返回</u></a><br/><br/>";

/*


*/
function eachPic($folder,$allPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$picFile)
{
	global $waterMarkFile,$wtCount; // $wtCount: watermark.php; $waterMarkFile: watermark.php, watermarkstr.php ls_syscode
	$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';

			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);		//字符串生成水印图片
					
						$sql_wt="select * from ls_products where productPic='".$file."' ";
						$query_wt=$opendb->query($sql_wt);
						$rows_wt=mysql_fetch_assoc($query_wt);
						$mynumfile=mysql_num_rows($query_wt);

						if( ($allPic==0 and $rows_wt['waterMark']==1 )||$mynumfile==0 || ifanimationgif($srcPic))
						{continue;}  // 动画，非全部水印但已经加过水印，文件不存在 则不生成水印 10-27
					
						//echo $srcPic." -- ".$desPic." -- ".$wtString." -- ".$wtType." -- ".$picFile;
						$wtCount++;
						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(file_exists($bakPic) && copy($bakPic,$oriPic ) )
					{echo " 水印已清除 ： ".$oriPic."<br/>";}
					else
					{echo " 水印清除失败 ： ".$oriPic."<br/>";}
					}
				}

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


?>
