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

	ob_clean();

    //检查字段存在，否则增加
	$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=mysqli_fetch_assoc($wt_query);
	$mynum=mysqli_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=mysqli_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'];
		$wtMargin=$_POST['wtMargin'];
		$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,$wtMargin,$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."', wtMargin='".$wtMargin."', 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'];
		$wtMargin=$wt_rows['wtMargin'];
		$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">
 <link rel="stylesheet" href="css/style2.css" />
<title>无标题文档</title>
<script language="JavaScript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/ls.js"></script>
<script type="text/javascript" src="js/jquery.colorpicker.js"></script>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="1" cellspacing="1">
  <tr>
    <td height="18" style="border-bottom:2px solid #00c0ef; padding-bottom:5px;"><div align="center"><font color="#3c8dbc" style="font-size:16px;">图片优化管理（水印）</font></div>
				</td>
  </tr>
  <tr>
    <td height="213" valign="top">
      <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="98%" border="0" align="center" cellpadding="10" cellspacing="1" style="background-color: #f0f0ee;">
           <tr bgcolor="#f0f0ee">
            <td align="center" height="37">测试图片</td>
            <td align="center"  height="37">水印内容</td>
          </tr>
          <tr>
            <td width="47%" align="center">
            	<?php 
			  //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;'  />";}
			  } 
			  ?>
            <!--图片：<select name="selectpic" id="select"  onchange="javascript:document.testpic.src=watermark.selectpic.value;watermark.testfile.value=watermark.selectpic.value;">
			      <?php while($test_rows=mysqli_fetch_assoc($test_query))
			  {
			  		$testfilefolder="../".getproductfolder($test_rows['productID'])."/".$BigFoldersProducts."/";
				   echo " <option value=\"".$testfilefolder.$test_rows['productPic']."\">".$test_rows['productName']."</option>\n";
			   }
			  ?>
		      </select> &nbsp; -->
            <br />
            <br />
            位置：
            <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>
 边距:&nbsp;
 <input name="wtMargin" type="text" value="<?php echo $wtMargin; ?>" style="width:30px;" /> 日期:&nbsp;
 <input name="date" type="text" value="<?php echo $wt_rows['date']; ?>" style="width:70px;" />
&nbsp;&nbsp;<br /></td>
            <td width="53%" align="left"  style="padding-left:50px"><div id="preview"><img src="<?=$wt_rows['wtImage']!=''?($wt_rows['wtImage']."?i=".rand()):'images/noimage.png'?>" name="pic"  border="1" id="pic" width="350" /><br />
            </div>
              <br />
              <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?>"  >
		    <p>
		      <input name="wtType" type="radio" id="radio7" value="string" <?php if($wtType == 'string'){echo "checked='checked'";} ?> />
		      文字 :&nbsp; 
		      <input type="text" name="wtString" value="<?php echo $wtString; ?>"/>
		      <input type="hidden" name="localpicFile" id="localpicFile"  value="<?=$localpicFile?>"/>
		      <!--字 体 :&nbsp;
<input type="text" name="wtFont" value="<?php echo $wtFont; ?>" />
<br />
颜 色 :&nbsp;
<input type="text" name="wtColor" value="<?php echo $wtColor; ?>"/>
<br />-->
		      字号 :&nbsp;
		      <input name="wtSize" type="text" value="<?php echo $wtSize;?>" style="width:20px;" size="1" />
              颜色 :&nbsp;
              <input type="text" name="wtColor" id="cp3text" style="width:28px; height:18px; border:0px; background-color:<?=$wtColor?>; color:<?=$wtColor?>;" value="<?=$wtColor?>" readonly="readonly" /> <img src="images/colorpicker.png" id="cp3" style="cursor:pointer"/>
		    </p></td>
          </tr>
<script type="text/javascript">
$("#cp3").colorpicker({
	fillcolor:true,
	success:function(o,color){
		$("#cp3text").css("background-color",color);
		$("#cp3text").css("color",color);
		$("#cp3text").val(color);
	}
});
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>
<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>
            <td colspan="2" align="center" height="35"><button name="submit2" type="submit" value="测试水印" onclick="javascript:watermark.action='watermarkstr.php';"/>测试水印</button></td>
          </tr>
          <tr>
            <td colspan="2" align="center">
            	<p>
            	<?php 
				$wt_sql2="select * from ls_watermark where ID= 1";
				$wt_query2=$opendb->query($wt_sql2);
				$wt_rows2=mysqli_fetch_assoc($wt_query2);
				$wtRange=$wt_rows2['wtRange']; 
				?>
            	<label>
            	  <input name="watermark[]" type="checkbox" class="plan01" id="lan1" onclick="unCheck01();" value="pro" checked="checked" <?php if(strpos($wtRange,"pro")) echo "checked=\"checked\""; ?> /> 
            	  产品</label> 
            	&nbsp; 
                <label>
                  <input name="watermark[]" type="checkbox" class="plan01" id="lan1" onclick="unCheck01();" value="art" checked="checked" <?php if(strpos($wtRange,"art")) echo "checked=\"checked\""; ?> /> 
                  文章</label> 
                &nbsp; 
                <label>
                  <input class="plan01" id="lan1" name="watermark[]" type="checkbox" value="wor" <?php if(strpos($wtRange,"wor")) echo "checked=\"checked\""; ?> onclick="unCheck01();" /> 
                  工厂</label> 
                &nbsp; 
            
              <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 type="hidden" name="testfile" id="hiddenField4" value="<?php echo $testfile ?>"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
            	<p>&nbsp;
            	  <button name="submit" type="submit" id="submit3" value="生成水印" />生成水印</button>
            	  <button name="submit3" type="submit" id="submit" value="清除全部"  onclick="javascript:document.watermark.clearallWatermark.value=1;"/>清除全部</button>
            	  <input type="hidden" name="clearallWatermark" id="clearallWatermark" value="0" />
       	    </p></td>
          </tr>
          <tr>
            <td colspan="2" style='padding-left:30px;'><div >1、“全部图片” 重新生成一遍全部图片； “新增图片”是指上次生成后新增的图片。<br />
		      2、没选择水印图片、没选择测试图片，将使用上次的。
			      <br />
			      3、如果给小图加水印，请使用png的水印图片。
			      <br />
			      4、可以先测试水印效果，然后生成
            </div></td>
          </tr>
        </table>
      </form>
    </td>
  </tr>
</table>
</body>
</html>
