<?php
if(!file_exists("../userfiles/".date('Ymd')))
		{
			//检查是否有该文件夹，如果没有就创建，并给予最高权限
			mkdir("../userfiles/".date('Ymd'), 0700);
		}
if(@$_FILES['image']['size']){
$uploaddir = "../userfiles/".date('Ymd')."/";//设置文件保存目录 注意包含/   
$type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型
$patch="userfiles/";//程序所在路径
   
   //获取文件后缀名函数
      function fileext($filename)
    {
        return substr(strrchr($filename, '.'), 1);
    }
   //生成随机文件名函数   
    function random($length)
    {
        $hash = 'tm_';
        $chars = '0123456789abcdefghijklmnopqrstuvwxyz';
        $max = strlen($chars) - 1;
        mt_srand((double)microtime() * 1000000);
            for($i = 0; $i < $length; $i++)
            {
                $hash .= $chars[mt_rand(0, $max)];
            }
        return $hash;
    }

   $a=strtolower(fileext($_FILES['image']['name']));
   //判断文件类型
   if(!in_array(strtolower(fileext($_FILES['image']['name'])),$type))
     {
        $text=implode(",",$type);
        echo "您只能上傳以下類型文件: ",$text,"<br>";
     }
   //生成目标文件的文件名   
   else{
    $filename=explode(".",$_FILES['image']['name']);
        do
        {
            $filename[0]=random(5); //设置随机数长度
            $name=implode(".",$filename);
			$imgs=$filename[0]."_s".".".$filename[1];
			$imgm=$filename[0]."_m".".".$filename[1];
            //$name1=$name.".Mcncc";
            $uploadfile=$uploaddir.$name;
			$imgs=$uploaddir.$imgs;
			$imgm=$uploaddir.$imgm;
        }

   while(file_exists($uploadfile));

        if (move_uploaded_file($_FILES['image']['tmp_name'],$uploadfile)){
            
			   $img=substr($uploadfile,3);
			   $imgs=substr($imgs,3);
			   $imgm=substr($imgm,3);
			   }
		else
			{
			  $img="";
			  $imgs="";
			  $imgm="";
			}
   }
}

class resizeimage
{
   //图片类型
   var $type;
   //实际宽度
   var $width;
   //实际高度
   var $height;
   //改变后的宽度
   var $resize_width;
   //改变后的高度
   var $resize_height;
   //是否裁图
   var $cut;
   //源图象
   var $srcimg;
   //目标图象地址[separator]
   var $dstimg;
   //临时创建的图象
   var $im;

function resizeimage($img, $wid, $hei,$c)
   {
       $this->srcimg = $img;
       $this->resize_width = $wid;
       $this->resize_height = $hei;
       $this->cut = $c;
       //图片的类型
       $this->type = substr(strrchr($this->srcimg,"."),1);
       //初始化图象
       $this->initi_img();
       //目标图象地址
       $this -> dst_img();
       //--
       $this->width = imagesx($this->im);
       $this->height = imagesy($this->im);
       //生成图象
       $this->newimg();
       ImageDestroy ($this->im);
   }
   
   
   
   function newimg()
   {
       //改变后的图象的比例
       $resize_ratio = ($this->resize_width)/($this->resize_height);
       //实际图象的比例
       $ratio = ($this->width)/($this->height);
       if(($this->cut)=="1")
       //裁图
       {
           if($ratio>=$resize_ratio)
           //高度优先
           {
               $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
               ImageJpeg ($newimg,$this->dstimg);
           }
           if($ratio<$resize_ratio)
           //宽度优先
           {
               $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
               ImageJpeg ($newimg,$this->dstimg);
           }
       }
       else
       //不裁图
       {
           if($ratio>=$resize_ratio)
           {
               $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
               ImageJpeg ($newimg,$this->dstimg);
           }
           if($ratio<$resize_ratio)
           {
               $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
               ImageJpeg ($newimg,$this->dstimg);
           }
       }
   }
   //初始化图象
   function initi_img()
   {
       if($this->type=="jpg")
       {
           $this->im = imagecreatefromjpeg($this->srcimg);
       }
       if($this->type=="gif")
       {
           $this->im = imagecreatefromgif($this->srcimg);
       }
       if($this->type=="png")
       {
           $this->im = imagecreatefrompng($this->srcimg);
       }
   }
   //图象目标地址
   function dst_img()
   {
       $full_length = strlen($this->srcimg);
       $type_length = strlen($this->type);
       $name_length = $full_length-$type_length;
       $name         = substr($this->srcimg,0,$name_length-1);
       $this->dstimg = $name."_s.".$this->type;
   }
   
}


class resizeimagem
{
   //图片类型
   var $type;
   //实际宽度
   var $width;
   //实际高度
   var $height;
   //改变后的宽度
   var $resize_width;
   //改变后的高度
   var $resize_height;
   //是否裁图
   var $cut;
   //源图象
   var $srcimg;
   //目标图象地址[separator]
   var $dstimg;
   //临时创建的图象
   var $im;

function resizeimagem($img, $wid, $hei,$c)
   {
       $this->srcimg = $img;
       $this->resize_width = $wid;
       $this->resize_height = $hei;
       $this->cut = $c;
       //图片的类型
       $this->type = substr(strrchr($this->srcimg,"."),1);
       //初始化图象
       $this->initi_img();
       //目标图象地址
       $this -> dst_img();
       //--
       $this->width = imagesx($this->im);
       $this->height = imagesy($this->im);
       //生成图象
       $this->newimg();
       ImageDestroy ($this->im);
   }
   
   
   
   function newimg()
   {
       //改变后的图象的比例
       $resize_ratio = ($this->resize_width)/($this->resize_height);
       //实际图象的比例
       $ratio = ($this->width)/($this->height);
       if(($this->cut)=="1")
       //裁图
       {
           if($ratio>=$resize_ratio)
           //高度优先
           {
               $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
               ImageJpeg ($newimg,$this->dstimg);
           }
           if($ratio<$resize_ratio)
           //宽度优先
           {
               $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
               ImageJpeg ($newimg,$this->dstimg);
           }
       }
       else
       //不裁图
       {
           if($ratio>=$resize_ratio)
           {
               $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
               ImageJpeg ($newimg,$this->dstimg);
           }
           if($ratio<$resize_ratio)
           {
               $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
               imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
               ImageJpeg ($newimg,$this->dstimg);
           }
       }
   }
   //初始化图象
   function initi_img()
   {
       if($this->type=="jpg")
       {
           $this->im = imagecreatefromjpeg($this->srcimg);
       }
       if($this->type=="gif")
       {
           $this->im = imagecreatefromgif($this->srcimg);
       }
       if($this->type=="png")
       {
           $this->im = imagecreatefrompng($this->srcimg);
       }
   }
   //图象目标地址
   function dst_img()
   {
       $full_length = strlen($this->srcimg);
       $type_length = strlen($this->type);
       $name_length = $full_length-$type_length;
       $name         = substr($this->srcimg,0,$name_length-1);
       $this->dstimg = $name."_m.".$this->type;
   }
   
}

include("qs.php");
require( '../data/config.php');
$conn=mysql_connect($db_host,$db_user,$db_pass);
mysql_query("set names 'utf8'");
mysql_select_db($db_name);
if (PHP_VERSION >= '5.1' && !empty($timezone))
{
    date_default_timezone_set($timezone);
}
$action=_get('action');
if($action=="")
$action=_post('action');
$id=_get('id');
if($id=="")
$id=_post('id');
?>
<!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" />
<META http-equiv="Pragma" content="no-cache" />
<LINK href="css/general.css" rel="stylesheet" type="text/css" />
<LINK href="css/main.css" rel="stylesheet" type="text/css" />
</HEAD><BODY>
<?php 
if($action=="del")
{
if(isset($_POST['chkkindid']) && is_array($_POST['chkkindid'])){
foreach($_POST['chkkindid'] as $tempid){
mysql_query("delete from tm_pic_list where id=".$tempid);
}
}
 echo "<script>location.href='pic_list.php';</script>";
 exit();
 }
elseif($action=="add")
{
$title=_post('title');
$des=_post('des');
$cid=_post('cid');
$date=date('Y-m-d');
if($img=="")
{
echo "<script>alert('没有上传相片，请重新上传');location.href='pic_list_w.php';</script>";
exit();
}
$tt = new resizeimage("../".$img, "200", "150", "1");
$ttm = new resizeimagem("../".$img, "1024", "768", "1");
$picsize=getimagesize("../".$img);
if($picsize[0]<1024)
   $imgm=$img;
mysql_query("insert into tm_pic_list(title,des,cid,date,img,imgs) values('$title','$des','$cid','$date','$imgm','$imgs')");
echo "<script>alert('上传相片成功');location.href='pic_list.php';</script>";
exit();
}
elseif($action=="edit")
{
$title=_post('title');
$des=_post('des');
$cid=_post('cid');
if(@$img=="")
$sql="update tm_pic_list set title='$title',des='$des',cid='$cid' where id='$id'";
else
{
$tt = new resizeimage("../".$img, "200", "150", "1");
$ttm = new resizeimagem("../".$img, "1024", "768", "1");
$picsize=getimagesize("../".$img);
if($picsize[0]<1024)
   $imgm=$img;
$sql="update tm_pic_list set title='$title',des='$des',cid='$cid',img='$imgm',imgs='$imgs' where id='$id'";
}
mysql_query($sql);
echo "<script>location.href='pic_list.php';</script>";
exit();
}
?>
<div class="h2">系统管理 >> 相薄管理 >> <?php if($id=="") echo "添加";else echo "编辑";?></div>
<div class="list-div">

<table cellspacing='1' cellpadding='3'>
<tr>
  <th align='left'><table width="100" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><div align="left">&nbsp;&nbsp;相薄管理</div></td>
        <td width="150">&nbsp;</td>
      </tr>
    </table></th>
</tr>
<tr><td align="left" valign="top" style="background:#F5F5F5;padding-bottom:8px;">
<TABLE cellSpacing=0 cellPadding=0 width="97.5%" border=0 CALLPADDING="0">
<?php 			  
if($action=="" and $id=="")
{
?>
<form id="form1" name="form1" method="post" action="pic_list_w.php" enctype="multipart/form-data">
			<TR BORDER="0" WIDTH="100%">
			                  <TD height="20"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">相片标题：
                                  <input name="title" type="text" id="title" size="50" />
              </span>			                  </TD>
    </TR>
			<TR BORDER="0" WIDTH="100%">
			                  <TD height="20" style="PADDING-LEFT: 8px; PADDING-TOP: 8px"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr style="display:none;">
                    <td>相片说明：
                    <textarea name="des" cols="50" rows="5" id="des"></textarea></td>
                    </tr>
                </table></TD>
    </TR>
			<TR BORDER="0" WIDTH="100%">
                <TD style="PADDING-LEFT: 8px; PADDING-TOP: 8px" vAlign=top 
                align=left bgColor=#ffffff height=20>所属相薄：
                  <select name="cid" id="art_class">
				  <option value="0">请选择相薄</option>
                    <?php
			
		   $sql="select * from tm_pic_class order by id";
		  $rsql=mysql_query($sql);
		  while($rs=mysql_fetch_object($rsql))
		  {
			  ?>
                    <option value="<?php echo $rs->id;?>"><?php echo $rs->title;?></option>
                    <?php
			  }
			  ?>
              </select></TD>
    </TR>
	
	<TR BORDER="0" WIDTH="100%">
              <TD style="PADDING-LEFT: 8px; PADDING-TOP: 8px" vAlign=top 
                align=left bgColor=#ffffff height=20> 上传相片：
			      <input name="image" type="file" id="image" />
			      <span class="post_date">(批量上传请到相薄管理中上传) </span></TD>
    </TR>
			<TR BORDER="0" WIDTH="100%">
                <TD style="PADDING-LEFT: 8px; PADDING-TOP: 8px" vAlign=top 
                align=left bgColor=#ffffff height=30><span class="help">
                  <input type="image" 
                        src="images/submit.gif" align="absmiddle" border="0" 
                        name="submit" onclick="return checkform(this.form)" />
                  <input name="action" type="hidden" id="action" value="add" />
                </span></TD>
              </TR>
 </form>
 <script language="javascript">
function checkform(theform)
{
if(theform.title.value=="")
  {
  alert("相片标题不能为空！");
  theform.title.focus();
  return false;
  }
if(theform.cid.value==0)
	{
		alert("相薄分类还没有选择哦,如果没有分类请先添加哦!!");
		theform.cid.focus();
		return false;
	}
return true;
}
</script>
 <?php 
 }
if($action=="" and $id<>"")
{
$sql="select * from tm_pic_list where id='$id'";
$rsql=mysql_query($sql);
$rs=mysql_fetch_object($rsql);
if(!$rs)
   {
   echo "<script>alert('无相关相片!');location.href='pic_list.php'</script>";
   exit();
   }
 ?>
 <form id="form1" name="form1" method="post" action="pic_list_w.php" enctype="multipart/form-data">
			<TR BORDER="0" WIDTH="100%">
			                  <TD height="20"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">相片标题：
                                  <input name="title" type="text" id="title" value="<?php echo $rs->title;?>" size="50" />
              </span>			                  </TD>
    </TR>
			
<TR BORDER="0" WIDTH="100%">
			                  <TD height="20" style="PADDING-LEFT: 8px; PADDING-TOP: 8px"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr style="display:none;">
                    <td>相片说明：
                    <textarea name="des" cols="50" rows="5" id="des"><?php echo $rs->des;?></textarea></td>
                    </tr>
                </table></TD>
    </TR>		
			<TR BORDER="0" WIDTH="100%">
                <TD style="PADDING-LEFT: 8px; PADDING-TOP: 8px" vAlign=top 
                align=left bgColor=#ffffff height=20>所属相薄：
                  <select name="cid" id="cid">
                    <?php
			
		   $sql="select * from tm_pic_class order by id";
		  $rsql=mysql_query($sql);
		  while($rs2=mysql_fetch_object($rsql))
		  {
			  ?>
                    <option value="<?php echo $rs2->id;?>" <?php if($rs->cid==$rs2->id) echo "selected";?>><?php echo $rs2->title;?></option>
                    <?php
			  }
			  ?>
              </select></TD>
    </TR>
	<TR BORDER="0" WIDTH="100%">
      <TD style="PADDING-LEFT: 3px; PADDING-TOP: 3px; background:#FFFFFF;" vAlign=top align=left height=30>
				<?php if($rs->imgs<>"") 
				{
				echo '<p><img src="../'.$rs->imgs.'" />';
				}
				?>&nbsp;
			    <br />
			    <span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">上传图片</span>：
	    <input name="image" type="file" id="image" /></TD>
    </TR>
			<TR BORDER="0" WIDTH="100%">
                <TD style="PADDING-LEFT: 8px; PADDING-TOP: 8px" vAlign=top 
                align=left bgColor=#ffffff height=30><span class="help">
                  <input type="image" 
                        src="images/submit.gif" align="absmiddle" border="0" 
                        name="submit" onclick="return checkform(this.form)" />
                  <input name="action" type="hidden" id="action" value="edit" />
                  <input name="id" type="hidden" id="id" value="<?php echo $id;?>" />
                </span></TD>
              </TR>
 </form>
<script language="javascript">
function checkform(theform)
{
if(theform.title.value=="")
  {
  alert("相片标题不能为空！");
  theform.title.focus();
  return false;
  }
if(theform.cid.value==0)
	{
		alert("相薄分类还没有选择哦,如果没有分类请先添加哦!!");
		theform.cid.focus();
		return false;
	}
return true;
}
</script>
 <?PHP }
 ?>
      </TABLE>
</td>
</tr></table></div>

<?php include "boot.php";
?>
</body></html>