<?php

namespace Home\Controller;
use Think\Controller;
class UploadController extends Controller {
    
    public function _initialize() {
        header("Content-Type:text/html; charset=utf-8");
        header("Content-Type:text/html; charset=utf-8");
        if (!I('post.session_id')) {
            E('哎哟！怎么到这里了?');
        }
    }

    
    Public function upUserPic () {
        if (!IS_POST) E('页面不存在');
        $upload = $this->_upUserPic();
        echo json_encode($upload);
    }
        
    Private function _upUserPic () {
        $config = array(
            'maxSize' => 3145728,
            'rootPath' => C('UPLOADS_PICPATH'),
            'savePath' => C('USER_PICPATH') . '/',
            'saveName' => array('uniqid',''),
            'exts' => array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub' => false,
        );

        $upload = new \Think\Upload($config);
        $info   =   $upload->upload();
        $info = $info['Filedata'];
        if(!$info) {
            return array('status' => 0, 'msg' => $upload->getError());
        }else{
            $uploadImg = $info['savepath'] . $info['savename'];
            $thumbImgUrl = C('UPLOADS_PICPATH').$info['savepath'] . $info['savename'];
            
            $imgThumb = new \Think\Image(); 
            $imgThumb->open($thumbImgUrl);
            $picFixArr=explode(',', C('USER_PIC_PREFIX'));
            foreach ($picFixArr as $pFixK => $pFixV) {
                $imSizeW = picSize($pFixK,'width','user');
                $imSizeH = picSize($pFixK,'height','user');
                $imgThumb->thumb($imSizeW,$imSizeH,\Think\Image::IMAGE_THUMB_CENTER)->save(C('UPLOADS_PICPATH').$info['savepath'] . $pFixV . $info['savename']);
                $path[]=C('UPLOADS_PICPATH').$info['savepath'] . $pFixV . $info['savename'];

            }
            return array(
            'status' => 1,
            'path' => $path,
            'savepath' => $uploadImg,
            'msg' => '上传成功！'
            );   
        }
    }
}?>
