<?php
ini_set('memory_limit','100M');
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);
}
function _get($str){  
$val = !empty($_GET[$str]) ? $_GET[$str] : null;  
return $val;  
}
function _post($str){  
$val = !empty($_POST[$str]) ? $_POST[$str] : null;  
return $val;  
}
mb_http_input("utf-8");
mb_http_output("utf-8");


if(!file_exists("../userfiles/".date('Ymd')))
		{
			//检查是否有该文件夹，如果没有就创建，并给予最高权限
			mkdir("../userfiles/".date('Ymd'), 0700);
		}
if($_FILES['Filedata']['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['Filedata']['name']));
   //判断文件类型
   if(!in_array(strtolower(fileext($_FILES['Filedata']['name'])),$type))
     {
        $text=implode(",",$type);
        echo "您只能上傳以下類型文件: ",$text,"<br>";
     }
   //生成目标文件的文件名   
   else{
    $filename=explode(".",$_FILES['Filedata']['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['Filedata']['tmp_name'],$uploadfile)){
			   $img=substr($uploadfile,3);
			   $imgs=substr($imgs,3);
			   $imgm=substr($imgm,3);
			   $temptitle=explode(".",$_FILES['Filedata']['name']);
			   $date=date('Y-m-d');
$tt = new resizeimage("../".$img, "200", "150", "1");
$ttm = new resizeimagem("../".$img, "1024", "768", "1");
$picsize=getimagesize("../".$img);
if($picsize[0]<1024)
   $imgm=$img;
$cid=_get('cid');
mysql_query("insert into tm_pic_list(title,cid,date,img,imgs) values('$temptitle[0]','$cid','$date','$imgm','$imgs')");
			   
			   }
		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;
   }
   
}
?>

