UeditorController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. //获取文件的原文件名 包括扩展名
  22. $yuanname= $request->file('upfile')->getClientOriginalName();
  23. $size = $request->file('upfile')->getSize();
  24. //获取文件的扩展名
  25. $kuoname=$request->file('upfile')->getClientOriginalExtension();
  26. //获取文件的绝对路径,但是获取到的在本地不能打开
  27. $filePath=$request->file('upfile')->getRealPath();
  28. //要保存的文件名 时间+扩展名
  29. $filename=time() . '_' . uniqid() .'.'.$kuoname;
  30. $img = Image::make($filePath);
  31. if ($img->getWidth()>720) {
  32. $img->resize(720, null, function ($constraint) {
  33. $constraint->aspectRatio();
  34. });
  35. $img->encode('jpg', 90);
  36. $data = $img->getEncoded();
  37. $imageUrl = Storage::put($path.$filename, $data);
  38. } else {
  39. $imageUrl = Storage::put($path.$filename, file_get_contents($filePath));
  40. }
  41. if($imageUrl){
  42. return [
  43. 'state' => 'SUCCESS',
  44. 'url' => config('customer.chxq_oss_url').$path.$filename,
  45. 'title' => $filename,
  46. 'original' => $yuanname,
  47. 'type' => $kuoname,
  48. 'size' => $size
  49. ];
  50. }else{
  51. return $this->response->error('图片上传失败,请重试', 500);
  52. }
  53. }else{
  54. return $this->response->error('图片上传失败,请重试', 500);
  55. }
  56. break;
  57. }
  58. }
  59. }