示例#1
0
 /**
  * Upload method helper
  *
  * @param null $name
  * @param null $file
  * @param array $params
  * @return array
  * @throws Exception
  */
 public function upload($name = null, $file = null, $params = ['afterUpload' => null, 'beforeUpload' => null, 'coords' => null, 'width' => null, 'height' => null, 'beforeUploadImage' => null])
 {
     Arr::mergeWithDefaultParams($params);
     $data = [];
     if ($file) {
         $data['file'] = $file;
     } elseif (Input::hasFile('file')) {
         $data['file'] = Input::file('file');
     } else {
         trigger_error("File doesn't exist.");
     }
     $data['name'] = $name;
     $data['params'] = $params;
     $data['response'] = false;
     $data['path'] = $this->relPath();
     $data['fullname'] = $data['file']->getClientOriginalName();
     $data['extension'] = $data['file']->getExtension();
     $data['mimeType'] = $data['file']->getMimeType();
     if (in_array($data['mimeType'], ['image/gif', 'image/jpeg', 'image/png'])) {
         $data['type'] = "image";
     } else {
         $data['type'] = "file";
     }
     if (!$data['name']) {
         $data['name'] = $data['file']->getClientOriginalName();
     } elseif (!preg_match('/\\.' . $data['extension'] . '$/i', $data['name'])) {
         $data['convert'] = true;
     } elseif (!preg_match('/\\./i', $data['name'])) {
         $data['name'] .= $data['name'] . '.' . $data['extension'];
     }
     if ($params['beforeUpload']) {
         $data = $params['beforeUpload']($data);
     }
     if ($data['type'] == 'image') {
         $img = Image::load($data['file']->getRealPath());
         if ($data['params']['coords']) {
             $coords = $data['params']['coords'];
             if (!is_array($coords)) {
                 $coords = json_decode(stripslashes($coords), true);
             }
             $img->crop($coords['x'], $coords['y'], $coords['x'] + $coords['w'], $coords['y'] + $coords['h']);
         } elseif ($data['params']['width'] && $data['params']['height']) {
             $img->resize($data['params']['width'], $data['params']['height']);
         } elseif ($data['params']['width']) {
             $img->fit_to_width($data['params']['width']);
         } elseif ($data['params']['height']) {
             $img->fit_to_width($data['params']['height']);
         }
         if ($params['beforeUploadImage']) {
             $img = $params['beforeUploadImage']($img);
         }
         @mkdir($data['path'], null, true);
         $data['response'] = $img->save(public_path($data['path'] . $data['name']));
     } else {
         $data['response'] = File::move($data['file']->getRealPath(), $data['path'] . $data['name']);
     }
     if ($params['afterUpload']) {
         $data = $params['afterUpload']($data);
     }
     return $data;
 }
 /**
  * Upload method helper
  *
  * @param null $name
  * @param null $file
  * @param array $params
  * @return array
  * @throws Exception
  */
 public function upload($name = null, $file = null, $params = ['path' => '', 'afterUpload' => null, 'beforeUpload' => null, 'coords' => null, 'width' => null, 'height' => null, 'beforeUploadImage' => null, 'preserveIfGif' => false])
 {
     Arr::mergeWithDefaultParams($params);
     $data = [];
     if ($file) {
         $data['file'] = $file;
     } elseif (Input::hasFile('file')) {
         $data['file'] = Input::file('file');
     } else {
         trigger_error("File doesn't exist.");
     }
     $data['name'] = $name;
     $data['params'] = $params;
     $data['response'] = false;
     $data['path'] = $this->relPath();
     $data['fullname'] = $data['file']->getClientOriginalName();
     $data['extension'] = $data['file']->getExtension() ?: $data['file']->getClientOriginalExtension();
     $data['mimeType'] = $data['file']->getMimeType();
     if (in_array($data['mimeType'], ['image/gif', 'image/jpeg', 'image/png'])) {
         $data['type'] = "image";
     } else {
         $data['type'] = "file";
     }
     if (!$data['name']) {
         $data['name'] = $data['file']->getClientOriginalName();
     } elseif (strpos($data['name'], '.') && !preg_match('/\\.' . $data['extension'] . '$/i', $data['name'])) {
         $data['convert'] = true;
     } elseif (!strpos($data['name'], '.')) {
         $data['name'] = $data['name'] . '.' . $data['extension'];
     }
     if ($params['beforeUpload']) {
         $data = $params['beforeUpload']($data);
     }
     if ($params['path']) {
         try {
             if (!is_dir(public_path($data['path'] . $params['path']))) {
                 mkdir(public_path($data['path'] . $params['path']), 0777, true);
             }
         } catch (Exception $e) {
             Api::handleError($e);
             return Api::responseJsonError($e);
         }
     }
     if ($data['extension'] == "gif" && $params['preserveIfGif']) {
         $data['response'] = $data['file']->move(public_path($data['path'] . $params['path']), $data['name']);
     } elseif ($data['type'] == 'image') {
         $img = Image::load($data['file']->getRealPath());
         if ($data['params']['coords']) {
             $coords = $data['params']['coords'];
             if (!is_array($coords)) {
                 $coords = json_decode(stripslashes($coords), true);
             }
             $img->crop($coords['x'], $coords['y'], $coords['x'] + $coords['w'], $coords['y'] + $coords['h']);
         } elseif ($data['params']['width'] && $data['params']['height']) {
             $img->resize($data['params']['width'], $data['params']['height']);
         } elseif ($data['params']['width']) {
             $img->fit_to_width($data['params']['width']);
         } elseif ($data['params']['height']) {
             $img->fit_to_width($data['params']['height']);
         }
         if ($params['beforeUploadImage']) {
             $img = $params['beforeUploadImage']($img);
         }
         $data['response'] = $img->save(public_path($data['path'] . $params['path'] . $data['name']));
     } else {
         $data['response'] = $data['file']->move(public_path($data['path'] . $params['path']), $data['name']);
     }
     if ($params['afterUpload']) {
         $data = $params['afterUpload']($data);
     }
     if (isset($data['path'], $data['name'])) {
         $data['relpath'] = self::cleanPath($data['path'] . $data['name']);
     }
     return $data;
 }