Пример #1
0
 function thumbnail()
 {
     $file = $this->req->files['file'];
     if (!empty($file)) {
         $name = $file['name'];
         $type = $file['type'];
         $size = $file['size'];
         $tmp = $file['tmp_name'];
         $error = $file['error'];
         if ($error != 0) {
             echo $this->load->view('uploadfail', array('info' => '上传图片出现系统错误'));
             exit;
         }
         if (!in_array($type, array('image/jpeg', 'image/png', 'image/bmp'))) {
             echo $this->load->view('uploadfail', array('info' => '只能上传图片类型:jpg,jpeg,png,bmp'));
             exit;
         }
         if ($size > 1024 * 1024 * 4) {
             echo $this->load->view('uploadfail', array('info' => '上传图片不能超过4M'));
             exit;
         }
         $pos = strrpos($name, '.');
         if ($pos > 0) {
             $suffix = substr($name, $pos);
         }
         if (empty($suffix)) {
             echo $this->load->view('uploadfail', array('info' => '文件名不符合要求'));
             exit;
         }
         $imgname = randImgName(30);
         $thumbnail = $imgname . '60x60' . $suffix;
         $result = move_uploaded_file($tmp, UPLOAD . 'thumbnail/' . $imgname . $suffix);
         header('Content-type:text/html');
         if ($result) {
             $this->load->model('img')->add($imgname . $suffix, $thumbnail);
             $lastid = $this->db->lastId();
             if (!empty($lastid)) {
                 imagecropper(UPLOAD . 'thumbnail/' . $imgname . $suffix, 60);
                 //生成略缩图
                 echo $this->load->view('uploadsuc', array('src' => HOSTNAME . "upload/thumbnail/" . $imgname . $suffix, 'img_id' => $lastid));
             } else {
                 if (unlink(UPLOAD . 'thumbnail/' . $imgname)) {
                     echo $this->load->view('uploadfail', array('info' => '插入数据库失败'));
                     exit;
                 }
             }
         } else {
             echo $this->load->view('uploadfail', array('info' => '移动文件失败'));
         }
     }
 }
Пример #2
0
 public function upload(Request $request)
 {
     $input = $request->all();
     $file_path = $message = '';
     $success = false;
     if (!$request->user()->can('image.upload')) {
         return response('您没有权限上传图片', 401);
     }
     $rules = array('file' => 'image|max:2000');
     $validation = Validator::make($input, $rules);
     if ($validation->fails()) {
         $message = '文件格式不正确';
         $data = array('success' => $success, 'msg' => $message, 'file_path' => $file_path);
         return response()->json($data);
     }
     $path = 'uploads/' . $request->user()->id . '/' . date('Ym');
     try {
         // File Upload
         if ($request->hasFile('file')) {
             $pic = $request->file('file');
             if ($pic->isValid()) {
                 $originalName = $pic->getClientOriginalName();
                 $newName = date('d') . '-' . md5(rand(1, 1000) . $originalName) . "." . $pic->getClientOriginalExtension();
                 $fileSize = $pic->getClientSize();
                 $request->file('file')->move($path, $newName);
                 $success = true;
                 $message = 'Upload Success';
                 $file_path = '/' . $path . '/' . $newName;
                 imagecropper(public_path() . $file_path, 64, 36, 'xs');
                 if ($request->crop == 1) {
                     imagecropper(public_path() . $file_path, 240, 135, 'small');
                     imagecropper(public_path() . $file_path, 800, 240, 'big');
                 }
                 Image::create(['user_id' => $this->currentUser->id, 'name' => $newName, 'alt' => $originalName, 'url' => $file_path, 'size' => $fileSize]);
             } else {
                 $message = 'The file is invalid';
             }
         } else {
             $message = 'No File';
         }
     } catch (\Exception $e) {
         $message = $e->getMessage();
     }
     $data = array('success' => $success, 'msg' => $message, 'file_path' => $file_path);
     return response()->json($data);
 }