UeditorController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Intervention\Image\Facades\Image;
  5. use Illuminate\Support\Facades\Storage;
  6. class UeditorController extends Controller
  7. {
  8. //
  9. public function index(Request $request)
  10. {
  11. $action = $request->action;
  12. switch ($action) {
  13. case 'config':
  14. $callback = $request->callback;
  15. $CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(public_path("config.json"))), true);
  16. return response($callback.'('.json_encode($CONFIG).')');
  17. break;
  18. case 'uploadimage':
  19. if($request->hasFile('upfile') && $request->file('upfile')->isValid()){
  20. // $path = $request->has('path') ? $request->path.'/' : date('Ym').'/';
  21. $path = $request['path'] ? $request['path'].'/' : date('Ym').'/';
  22. //获取文件的原文件名 包括扩展名
  23. $yuanname= $request->file('upfile')->getClientOriginalName();
  24. $size = $request->file('upfile')->getSize();
  25. //获取文件的扩展名
  26. $kuoname=$request->file('upfile')->getClientOriginalExtension();
  27. //获取文件的绝对路径,但是获取到的在本地不能打开
  28. $filePath=$request->file('upfile')->getRealPath();
  29. //要保存的文件名 时间+扩展名
  30. $filename=time() . '_' . uniqid() .'.'.$kuoname;
  31. $img = Image::make($filePath);
  32. if ($img->getWidth()>480) {
  33. $img->resize(480, null, function ($constraint) {
  34. $constraint->aspectRatio();
  35. });
  36. $img->encode('jpg', 90);
  37. $data = $img->getEncoded();
  38. $imageUrl = Storage::put($path.$filename, $data);
  39. } else {
  40. $imageUrl = Storage::put($path.$filename, file_get_contents($filePath));
  41. }
  42. if($imageUrl){
  43. return [
  44. 'state' => 'SUCCESS',
  45. 'url' => config('customer.chxq_oss_url').$path.$filename,
  46. 'title' => $filename,
  47. 'original' => $yuanname,
  48. 'type' => $kuoname,
  49. 'size' => $size
  50. ];
  51. }else{
  52. return $this->response->error('图片上传失败,请重试', 500);
  53. }
  54. }else{
  55. return $this->response->error('图片上传失败,请重试', 500);
  56. }
  57. break;
  58. }
  59. }
  60. }