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

	$submit=$_POST['submit'];
	$wtCount=0; // 添加水印计数器
	$wtString=$_POST['wtString'];
	$wtFont=$_POST['wtFont'];
	$wtSize=$_POST['wtSize'];
	$wtMargin=$_POST['wtMargin'];
	$wtColor=$_POST['wtColor'];
	$wtPosition=$_POST['wtPosition'];
	$allPic=$_POST['allPic'];
	$wtType=$_POST['wtType'];
	//$picFile=$_FILES['picFile']['tmp_name'];
	
	$watermark = $_POST['watermark'];  //获取的多选框的值，为一个数组，通过下面的foreach对数据进行操作
	if($watermark != null){
		$watermarks = "w";
		foreach($watermark as $employee_id) {
			$watermarks .= $employee_id;
		}
	}

	$wPic="";// watermarkfile in ls-admin/images
	$clearallWatermark=$_POST['clearallWatermark'];
	
	include "widget/watermark_createwtpic.php";
	
	if(strpos($watermarks,"pro")){   //生成产品水印
		$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\">返回</a><br/><br/>";
	
		while($cate_rows=mysqli_fetch_assoc($cate_query))
		{
			$cateid=$cate_rows['ID'];
				//大类
				$sqlstr1="select * from ls_products where midcateID =".$cateid." ";
				$query1=$opendb->query($sqlstr1);
				$mynum1=mysqli_num_rows($query1);
				
				if($mynum1!=0)//有中类
				{
					while($myrows1=mysqli_fetch_assoc($query1))
					{
						$sqlstr2="select * from ls_products where subcateID =".$myrows1['ID']." ";
						$query2=$opendb->query($sqlstr2);
						$mynum2=mysqli_num_rows($query2);
						if($mynum2!=0) //有小类
						{
							while($myrows2=mysqli_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,$wtMargin,$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,$wtMargin,$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,$wtMargin,$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,$wtMargin,$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,$wtMargin,$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,$wtMargin,$wtColor,$wtPosition,$wtType,$smallWTPic);
						}
					}
				}
		}
		
		if($clearallWatermark==1)
		{
			$clearsql="update ls_products set waterMark =0";  //清除水印标志
			$clear_query=$opendb->query($clearsql);
			$clearsql="update ls_watermark set wtImage='".$waterMarkFile."', wtFont='".$wtFont."', wtSize='".$wtSize."', wtMargin='".$wtMargin."', wtColor='".$wtColor."', wtPosition='".$wtPosition."', wtRange='".$watermarks."', date='".date('Y-m-d H:i:s')."' , wtOperate='生成' 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(strpos($watermarks,"art")){   //生成文章水印
		
		$folder = "kindeditor-4.1.10/attached/image";
		foreach(file_list($folder) as $folder_val){
			
			if($clearallWatermark==1)		//clear all watermark
			{clearEachPic($folder_val);
			}else{
				eachPic2($folder_val,$allPic,$wtString,$wtFont,$wtSize,$wtMargin,$wtColor,$wtPosition,$wtType,$picFile);
			}
		}
	}
	
	if(strpos($watermarks,"wor")){   //生成工厂水印
	
		$folder = "../".$WorkShopFolders."/".$BigFoldersProducts;  //大图地址
		
		if($clearallWatermark==1)		//clear all watermark
		{clearEachPic($folder);}
		else
		{
			eachPic2($folder,$allPic,$wtString,$wtFont,$wtSize,$wtMargin,$wtColor,$wtPosition,$wtType,$picFile);
		}

		$folder = "../".$WorkShopFolders."/".$SmallFoldersProducts;  //小图地址
		if($clearallWatermark==1)
		{clearEachPic($folder);}
		else
		{
			if($waterMarkThumbnails && file_exists($smallWTPic)) 
			{
				eachPic2($folder,$allPic,$wtString,$wtFont,$wtSize,$wtMargin,$wtColor,$wtPosition,$wtType,$smallWTPic);
			}
		}
		
	}

	if($submit=="生成水印" && $wtCount!=0)  //没有计数没有生成
	{
		if($wtType=="pic" )
		{
			
			$modi_sql="update ls_watermark set wtImage='".$waterMarkFile."', wtFont='".$wtFont."', wtSize='".$wtSize."', wtMargin='".$wtMargin."', wtColor='".$wtColor."', wtPosition='".$wtPosition."', wtRange='".$watermarks."', 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."', wtRange='".$watermarks."', 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\">返回</a><br/><br/>";

/*
产品添加水印
*/
function eachPic($folder,$allPic,$wtString,$wtFont,$wtSize,$wtMargin,$wtColor,$wtPosition,$wtType,$picFile)
{
	global $waterMarkFile,$wtCount; // $wtCount: watermark.php; $waterMarkFile: watermark.php, watermarkstr.php ls_syscode
	$opendb=new mydb();
	
	$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=mysqli_fetch_assoc($query_wt);
						$mynumfile=mysqli_num_rows($query_wt);
						
						//echo $srcPic." -- ".$desPic." -- ".$wtString." -- ".$wtType." -- ".$picFile."<br />";
						//echo $allPic." -- ".$mynumfile."<br />";
						//echo $folder."2222".$desPic."<br />";
						//echo $allPic."-".$rows_wt['waterMark']."-".$mynumfile."-".ifanimationgif($srcPic)."<br />";
						
						if(($allPic==0 and $rows_wt['waterMark']==1 )||$mynumfile==0 || ifanimationgif($srcPic))
						{continue;}  // 动画，非全部水印但已经加过水印，文件不存在 则不生成水印 10-27
						
						$wtCount++;
						if(addWatermark($srcPic,$desPic,$wtString,$wtFont,$wtSize,$wtMargin,$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 eachPic2($folder,$allPic,$wtString,$wtFont,$wtSize,$wtMargin,$wtColor,$wtPosition,$wtType,$picFile)
{
	global $waterMarkFile,$wtCount; // $wtCount: watermark.php; $waterMarkFile: watermark.php, watermarkstr.php ls_syscode
	$opendb=new mydb();
	
	$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);		//字符串生成水印图片

						//if($allPic===0)
						if($allPic==0 ||$mynumfile==0 || ifanimationgif($srcPic))
						{continue;}  // 动画，非全部水印但已经加过水印，文件不存在 则不生成水印 10-27

						$wtCount++;
						if(addWatermark($srcPic,$desPic,$wtString,$wtFont,$wtSize,$wtMargin,$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();
	
	$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 " 目录".$picFolder."不存在！<br/>";
	}
}

//得到某目录下的所以文件夹名称
function file_list($path)
{
	$result = array();
	if (is_dir($path) && $handle = opendir($path)) {
		while (FALSE !== ($file = readdir($handle))) {
			if ($file == '.' || $file == '..') continue 1;
			$real_path = str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $path.DIRECTORY_SEPARATOR.$file); //realpath($path.DIRECTORY_SEPARATOR.$file);//得到当前文件的全称路径
			$result[] = $real_path;
			//if (is_dir($real_path)) 
				//$result = array_merge($result, file_list($real_path));
		}
		closedir($handle);	
	}
	return $result;
}

?>
