<?
/*
水印全部生成，水印清除
水印生成界面,测试水印生成在  watermarkstr.php
ls_watermark 至少有一条记录吗，且其ID=1
$GLOBALS['wPic']   constant文件 $waterMarkPic定义的 路径，在ls_syscode addWatermark函数加上了扩展名
*/
session_start(); 

	ob_clean();
	require_once('../ls_admin/inc/conn.php');
	require_once('../ls_admin/inc/constant.php');
	require_once('../ls_admin/inc/ls_syscode.php');
	require_once('../ls_admin/inc/ls_inccode.php');

	$opendb=new mydb();

	require_once('../ls_admin/inc/char_set.php');

    //检查字段存在，否则增加
	$openck=new cktb();
	if($openck->checkfield("ls_watermark","wtOperate","ALTER TABLE `ls_watermark` ADD `wtOperate` VARCHAR( 4 ) NOT NULL COMMENT '水印操作类型：测试，生成，null' AFTER `date` "))	$ls_msg = $ls_msg .$openck->ls_msg;
	if($openck->checkfield("ls_watermark","wtTestfile","ALTER TABLE `ls_watermark` ADD `wtTestfile` VARCHAR( 255 ) NOT NULL COMMENT '上次测试图片文件' AFTER `wtOperate` "))	$ls_msg = $ls_msg .$openck->ls_msg;
	
	$wt_sql="select * from ls_watermark where ID= 1";
	$wt_query=$opendb->query($wt_sql);
	$wt_rows=mysql_fetch_assoc($wt_query);
	$mynum=mysql_num_rows($wt_query);
	if($mynum==0)//没有水印记录则添加
	{
		$wt_insert="insert into ls_watermark (ID) value (1)";
		$wt_query=$opendb->query($wt_insert);
	}
	$submit2=$_POST['submit2'];
	$selectpic=$_POST['selectpic'];
	
	$watermark = $_POST['watermark'];  //获取的多选框的值，为一个数组，通过下面的foreach对数据进行操作
	
	if($watermark != null){
		$watermarks = "w";
		foreach($watermark as $employee_id) {
			$watermarks .= $employee_id;
		}
	}
	
	//没有选择测试，使用上次的
//$testfile=($_POST['testfile']!=''&&file_exists($_POST['testfile']))?$_POST['testfile']:$wt_rows['wtTestfile'];

	include "widget/watermark_createwtpic.php";
	
	$checkstring="checked";		//default string
	if($_GET['desPic'])
	{$desPic=urldecode($_GET['desPic']);}  // flash this page then get desPic
	//没有选择水印，使用上次的
	$picFile=$_FILES['picFile']['tmp_name']==''?$wt_rows['wtImage']:$_FILES['picFile']['tmp_name'];
	
	$test_sql="select * from ls_products where productID is not null and productName is not null and productPic is not null and enable=1 limit 0,15 ";
	//$test_sql="select * from ls_products";
	$test_query=$opendb->query($test_sql);
	$test_rows=mysql_fetch_assoc($test_query);
	$testfilefolder="../".getproductfolder($test_rows['productID'])."/".$BigFoldersProducts."/";
	if($testfile=="")
	{
		$testfile=$testfilefolder.$test_rows['productPic'];
	}
	if($submit2=="测试水印")
	{
		$wtString=$_POST['wtString'];
		$wtFont=$_POST['wtFont'];
		$wtSize=$_POST['wtSize'];
		$wtColor=$_POST['wtColor'];
		$wtPosition=$_POST['wtPosition'];
		$allPic=$_POST['allPic'];
		$wtType=$_POST['wtType'];
		$srcPic=$testfile;
		$localpicFile=$_POST['localpicFile'];

		if($picFile!='' && $testfile!='' && file_exists($testfile) && file_exists($picFile))  //如果没有测试图片或者水印图片，不执行
		{
			//echo $picFile."----".$testfile;

			list($srcwidth, $srcheight, $srctype, $srcattr) = getimagesize($testfile);//获取文件类型
			if($srctype==1)
			{
				$desPic="images/testwtk.gif";
			}
			if($srctype==2)
			{ 
				$desPic="images/testwtk.jpg";
			}
			if($srctype==3)
			{
				$desPic="images/testwtk.png";
			}
			
			addWatermark($srcPic,$desPic,$wtString,$wtFont,$wtSize,$wtColor,$wtPosition,$wtType,$picFile);
			//更新水印操作记录 13-9-29
			//$waterMarkFile  global var from ls_syscode
			$modi_sql="update ls_watermark set wtImage='".$waterMarkFile."', wtString='".$wtString."', wtFont='".$wtFont."', wtSize='".$wtSize."', wtColor='".$wtColor."', wtPosition='".$wtPosition."', wtRange='".$watermarks."', date='".date('Y-m-d H:i:s')."' , wtOperate='测试', wtTestfile='".$srcPic."' where ID=1  ";
			//echo $modi_sql;
			$modi_query=$opendb->query($modi_sql);

			
			$ls_msg= " 测试水印已生成： ".$desPic." <a href='watermarkstr.php?desPic=".urlencode($desPic)."'> 请刷新本页查看</a>";
		}
		else
		{
			$ls_msg= "没有选择水印图片或者测试图片";	
		}

			
	}
	else
	{
		$wtString=$wt_rows['wtString'];
		$wtFont=$wt_rows['wtFont'];
		$wtSize=$wt_rows['wtSize'];
		$wtColor=$wt_rows['wtColor'];
		$wtPosition=$wt_rows['wtPosition'];
		$date=$wt_rows['date'];
		$wtImage=$wt_rows['wtImage']; 
		if($wtString==NULL&&$wtImage!=NULL)
		{$wtType="pic";}
	}
	
	if($wtType=="pic")
	{$checkpic="checked";}
	else
	{$checkstring="checked";}
	//echo 		$localpicFile;
?>
<!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>
<script language="JavaScript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/ls.js"></script>
</head>
<body>
<table width="96%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#6396d6">
  <tr>
    <td height="18" background="../images/admin_bg.gif"><div align="center"><strong><font color="#ffffff">图片优化管理（水印）</font></strong></div></td>
  </tr>
  <tr>
    <td height="213" valign="top" bgcolor="#dee7ff">
      <table border="0" width="90%" align="center">
        <tr>
          <td><?=$ls_msg?></td>
        </tr>
      </table>
      <form name="watermark" id="watermark" action="watermark.php" method="post" enctype="multipart/form-data"  >
      	<table width="90%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
          <tr bgcolor="#B9DAFF">
            <td rowspan="3" align="center">
            	<? 
			  //echo $desPic;
			  if($desPic!="") 
			  {	 				  
			  	  if(file_exists($desPic))
				  //."?".date('Y-m-d H:i:s')." 水印刷新
				  { echo  "<img src=\"".$desPic."?".date('Y-m-d H:i:s')."\" name=\"testpic\"  border=\"0\" style='margin:15;'  />"; }
				  else
				  {	  echo  "<img src=\"images/noimage.png\" name=\"testpic\"  border=\"0\" style='margin:15;' />" ;}
				}
			  else
			  {  
				  if(file_exists($testfile))
				  { echo  "<img src=\"".$testfile."?i=".rand()."\" name=\"testpic\"  border=\"0\" style='margin:15;'  />"; }
				  else
				  {	 echo  "<img src=\"images/noimage.png\" name=\"testpic\"  border=\"0\" style='margin:15;'  />";}
			  } 
			  ?>
            </td>
            <td align="center"><div id="preview"><img src="<?=$wt_rows['wtImage']!=''?($wt_rows['wtImage']."?i=".rand()):'images/noimage.png'?>" name="pic"  border="1" id="pic" width="300" /></div></td>
          </tr>
<script type="text/javascript">  
function preview(file)
{
var prevDiv = document.getElementById('preview');
if (file.files && file.files[0])
{
var reader = new FileReader();
reader.onload = function(evt){
prevDiv.innerHTML = '<img src="' + evt.target.result + '" width="300" border="1" name="pic" />';
}  
reader.readAsDataURL(file.files[0]);
}
else  
{
prevDiv.innerHTML = '<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + file.value + '\'"></div>';
}
}
</script>
          <tr bgcolor="#B9DAFF">
            <td align="center" height="45" ><input name="wtType" type="radio" id="radio8" onclick="javascript:document.watermark.picFile.enabled;" value="pic" <?php if($wtType == 'pic' || $wtType == ''){echo "checked='checked'";} ?>  />
			    图片:
			    <input name="picFile" type="file" id="fileField" onchange="preview(this);" value="<?=$localpicFile?>"  ></td>
          </tr>
          <tr bgcolor="#B9DAFF">
            <td rowspan="2" align="center"><input name="wtType" type="radio" id="radio7" value="string" <?php if($wtType == 'string'){echo "checked='checked'";} ?> />
文字 :&nbsp; 
  <input type="text" name="wtString" value="<? echo $wtString; ?>"/>
  <input type="hidden" name="localpicFile" id="localpicFile"  value="<?=$localpicFile?>"/>
  <br />
  <br />
<!--字 体 :&nbsp;
<input type="text" name="wtFont" value="<? echo $wtFont; ?>" />
<br />
颜 色 :&nbsp;
<input type="text" name="wtColor" value="<? echo $wtColor; ?>"/>
<br />-->
字体大小 :&nbsp;
<input type="text" name="wtSize" value="<? echo $wtSize;?>" /></td>
          </tr>
          <tr bgcolor="#B9DAFF" align="center">
            <td height="45"><!--图片：<select name="selectpic" id="select"  onchange="javascript:document.testpic.src=watermark.selectpic.value;watermark.testfile.value=watermark.selectpic.value;">
			      <? while($test_rows=mysql_fetch_assoc($test_query))
			  {
			  		$testfilefolder="../".getproductfolder($test_rows['productID'])."/".$BigFoldersProducts."/";
				   echo " <option value=\"".$testfilefolder.$test_rows['productPic']."\">".$test_rows['productName']."</option>\n";
			   }
			  ?>
		      </select> &nbsp; -->
 位置：<select name="wtPosition" id="position"  >
	<option value="1" <?php if($wtPosition == 1){ echo "selected";} ?>>左上</option>
    <option value="2" <?php if($wtPosition == 2){ echo "selected";} ?>>中上</option>
    <option value="3" <?php if($wtPosition == 3){ echo "selected";} ?>>右上</option>
    <option value="4" <?php if($wtPosition == 4){ echo "selected";} ?>>中心</option>
    <option value="5" <?php if($wtPosition == 5){ echo "selected";} ?>>左下</option>
    <option value="6" <?php if($wtPosition == 6){ echo "selected";} ?>>中下</option>
    <option value="7" <?php if($wtPosition == 7){ echo "selected";} ?>>右下</option>
		  </select>
<input name="all" type="radio" id="radio2" value="1" checked="checked" onclick="javascript:document.watermark.allPic.value=0;"/>
新增图片
<input name="all" type="radio" id="radio" value="1" onclick="javascript:document.watermark.allPic.value=1;"/>
全部图片
<input type="hidden" name="allPic" id="hiddenField" value="0" />

<input name="submit2" type="submit" value="测试水印" onclick="javascript:watermark.action='watermarkstr.php';"/>
<input type="hidden" name="testfile" id="hiddenField4" value="<? echo $testfile ?>"/></td>
          </tr>
          <script>
			function plan01(){
				var str = '';
			   $(".plan01").each(function(){
					if($(this).is(':checked')){
						str += $(this).val();
					}
				});
			   return str;
			};
			
			function checkPlan01(){
				$(".plan01").each(function(){
					if($(".checkPlan01").is(':checked')){
						$(this).attr("checked",true);
					}else{
						$(this).attr("checked",false);
					}
				});
			}
			
			function unCheck01(){
				if($(".checkPlan01").is(':checked')){
					$(".checkPlan01").attr("checked",false);
				}
			}
			</script>
          <tr bgcolor="#B9DAFF">
            <td colspan="2" align="center" height="35">
            	<?php 
				$wt_sql2="select * from ls_watermark where ID= 1";
				$wt_query2=$opendb->query($wt_sql2);
				$wt_rows2=mysql_fetch_assoc($wt_query2);
				$wtRange=$wt_rows2['wtRange']; 
				?>
            	<label><input class="plan01" id="lan1" name="watermark[]" type="checkbox" value="pro" <? if(strpos($wtRange,"pro")) echo "checked=\"checked\""; ?> onclick="unCheck01();" /> 产品</label> &nbsp; 
                <label><input class="plan01" id="lan1" name="watermark[]" type="checkbox" value="art" <? if(strpos($wtRange,"art")) echo "checked=\"checked\""; ?> onclick="unCheck01();" /> 文章</label> &nbsp; 
                <label><input class="plan01" id="lan1" name="watermark[]" type="checkbox" value="wor" <? if(strpos($wtRange,"wor")) echo "checked=\"checked\""; ?> onclick="unCheck01();" /> 工厂</label> &nbsp; 
            </td>
          </tr>
          <tr bgcolor="#B9DAFF">
            <td colspan="2" align="center" height="45">上次<? echo $wt_rows['wtOperate']; ?>日期:&nbsp;
              <input type="text" name="date" value="<? echo $wt_rows['date']; ?>" />               
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input name="submit" type="submit" id="submit3" value="生成水印" />
                <input name="submit3" type="submit" id="submit" value="清除全部水印"  onclick="javascript:document.watermark.clearallWatermark.value=1;"/>
                <input type="hidden" name="clearallWatermark" id="clearallWatermark" value="0" /></td>
          </tr>
          <tr bgcolor="#B9DAFF">
            <td colspan="2" style='padding-left:30px;'><div >1、“全部图片”指重新全部生成一遍水印；“新增图片”是指只生成上次日期以后新增图片的水印生成。<br />
		      2、没选择水印图片、没选择测试图片，将使用上次的。
			      <br />
			      3、如果给小图加水印，请使用png的水印图片。
			  </div></td>
          </tr>
        </table>
      </form>
    </td>
  </tr>
</table>
</body>
</html>
