<?php
namespace home\Controller;
use Think\Controller;
class UploadController extends Controller {

	/**
	 * [attach 附件上传]
	 * @return [type] [description]
	 */
public function img(){
		$type = I('request.type','question_img','trim');
		if(!in_array($type,array('question_img','article_img','kindeditor_img','tag_img','avatar_img','category_img'))) return false;
		if(IS_POST){			
			if (!empty($_FILES[$type]['name']))
			{
					$maxSize   = 2097152;
					switch ($type)
					{
					case 'question_img':
					  $rootPath  = C('UU_IMG_QUESTION_DIR'); 
					  break;  
					case 'article_img':
					   $rootPath  = C('UU_IMG_ARTICLE_DIR');
					    $maxSize   = 2097152;
					  break;
					case 'kindeditor_img':
					   $rootPath  = C('UU_IMG_ARTICLE_DIR');
					    $maxSize   = 2097152;
					  break;
					case 'category_img':
					   $rootPath  = C('UU_IMG_CATEGORY_DIR'); 
					  break;
					case 'tag_img':
					   $rootPath  = C('UU_IMG_TAG_DIR'); 
					  break;
					case 'avatar_img':
					   $rootPath  = C('UU_IMG_AVATAR_DIR'); 
					  break;
					}	
						$upload = new \Think\Upload();// 实例化上传类
						$upload->maxSize   =     $maxSize ;// 设置附件上传大小
						$upload->autoSub   =     false;				
						$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
						$upload->rootPath  =     $rootPath ; // 设置附件上传根目录
						$upload->savePath  =     date('Y/m/'); // 设置附件上传（子）目录
						$upload->saveName = array('uniqid',''); 
						// 上传文件 
						$info   =   $upload->uploadOne($_FILES[$type]);
						if(!$info) {// 上传错误提示错误信息
							$ajax['status']  = 0;
							$ajax['content'] =$upload->getError();
							$this->ajaxReturn($ajax);
						}else{// 上传成功
							$info['savename']=$upload->savePath.$info['savename'];
							$ajax['status']  = 1;
							$ajax['content']['savename']   = $info['savename'];
							$ajax['content']['src']        = __ROOT__.'/'.$rootPath.$info['savename'];
							$kindeditor['error']=0;
							$kindeditor['url'] = __ROOT__.'/'.$rootPath.$info['savename'];
							//生成缩略图
							$image = new \Think\Image(); 
							$image->open($rootPath.$info['savename']);
							$image->thumb(600,600)->save($rootPath.$info['savename']);
							if ($type=='question_img')
							{
							$thumb=$rootPath.$info['savename'];
							$thumb=$thumb.'.thumb.'.substr(strrchr($thumb,'.'),1);
							$image->thumb(100,100)->save($thumb);
							}
							//生成缩略图完毕
							if ($type=='kindeditor_img')
							{
							$this->ajaxReturn($kindeditor);
							}
							$this->ajaxReturn($ajax);
						}				
			}
			else
			{
				$ajax['status']  = 0;
				$ajax['content'] ="参数非法";
				$this->ajaxReturn($ajax);
			}
		}
}
public function falsh(){
		$uid=session('uu.uid');
		if (empty($uid))
		{
			$ajax['status']  = 0;
			$ajax['content'] ="参数非法";
			$this->ajaxReturn($ajax); 
		}
		else
		{
			$date = date('Y/m/');
			$save_avatar=C('UU_IMG_AVATAR_DIR').$date;//图片存储路径
			if(!is_dir($save_avatar)){
				mkdir($save_avatar,0777,true);
			}
			$savePicName = $uid.'.jpg';
			$filename = $save_avatar.$savePicName;
			$pic=base64_decode($_POST['pic1']);
			file_put_contents($filename,$pic);
		 	//生成缩略图
			$image = new \Think\Image();
			$image->open($filename)->thumb(150,150)->save($filename);
			/* $size = explode(',',C('UU_IMG_AVATAR_SIZE'));//50,100
			foreach ($size as $val) {
				$image->open($filename)->thumb($val,$val,3)->save($save_avatar.$uid."_".$val."x".$val.".jpg");
			} */
			D('User')->edit_avatar($uid,$date.$savePicName);
			D('Task')->exe_user_avatar($uid);//完成上传头像任务		
			$ajax['status']  = 1;
			$ajax['picUrl'] = $filename;
			$this->ajaxReturn($ajax);
		}
				
}

}
?>