图片上传

/*图片上传*/
    public function upload(){
        $file = $_FILES['mypic'];
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 2*1024*1024;
        $upload->rootPath  = './Uploads/'; // 设置附件上传根目录
        $upload->savePath  = 'Advert/yyzz/'; // 设置附件上传(子)目录
        $upload->exts     = array('jpg', 'jpeg','png');
        $info = $upload->uploadOne($file);
        $infourl='./Uploads/'.$info['savepath'].$info['savename'];
        $image = new \Think\Image();
        $image->open($infourl);//将图片裁剪为400x400并保存为corp.jpg
        $width = $image->width(); // 返回图片的宽度
        $height = $image->height(); // 返回图片的高度
        $iw = $ih = 150;
        if($iw>$width){
            $iw = $width;
        }
        if($ih>$height){
            $ih = $height;
        }
        if($width>150 || $height>150){
            $image->thumb($iw, $ih,\Think\Image::IMAGE_THUMB_CENTER)->save($infourl);
        }
        if($info){
            $data['imgurl']='./Uploads/'.$info['savepath'].$info['savename'];
            $res=M('image')->add($data);
            if($res){
                exit('{"name":"'.$info['savename'].'","imgurl":"'.$data['imgurl'].'","imgid":"'.$res.'"}');
            }
        }else{
            exit;
        }

    }

 

posted @ 2015-12-14 16:00  FinnYY  阅读(162)  评论(0编辑  收藏  举报