UploadController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Storage;
  6. use Intervention\Image\Facades\Image;
  7. use Symfony\Component\Debug\Exception\FatalThrowableError;
  8. class UploadController extends Controller
  9. {
  10. public function uploadImage(Request $request)
  11. {
  12. if ($request->hasFile('image') && $request->file('image')->isValid()) {
  13. $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/';
  14. $kuoname = $request->file('image')->getClientOriginalExtension();
  15. $filePath = $request->file('image')->getRealPath();
  16. Log::debug('文件类型'.$kuoname);
  17. try{
  18. // if (in_array($kuoname, ['jpg', 'jpeg', 'gif', 'png'])) {
  19. // $width = Image::make($filePath)->width();
  20. // $height = Image::make($filePath)->height();
  21. // //要保存的文件名 时间+扩展名
  22. // $filename = time() . '_' . uniqid() . '.' . $kuoname;
  23. // $fileurl = $filename . '?' . $width . '_' . $height;
  24. // } else {
  25. //要保存的文件名 时间+扩展名
  26. $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname;
  27. // }
  28. $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
  29. Log::debug('upload_url:' . $imageUrl);
  30. if ($imageUrl) {
  31. return $this->jsonSuccess(['url' => config('customer.chxq_oss_url') . $path . $fileurl]);
  32. } else {
  33. return $this->jsonError('图片上传失败,请重试');
  34. }
  35. }catch (\Exception $exception){
  36. Log::error('upload-exception:'.$exception->getMessage());
  37. return $this->jsonError('图片上传失败,请重试');
  38. }catch (\Throwable $throwable){
  39. Log::error('upload-error:'.$throwable->getMessage());
  40. return $this->jsonError('图片上传失败,请重试');
  41. }
  42. } else {
  43. return $this->jsonError('仅支持图片上传,请重试');
  44. }
  45. }
  46. public function uploadImages(Request $request)
  47. {
  48. $files = $request->file('image');
  49. $fileCount = count($files);
  50. $urls = [];
  51. $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/';
  52. for ($i = 0; $i < $fileCount; $i++) {
  53. $kuoname = $files[$i]->getClientOriginalExtension();
  54. $filePath = $files[$i]->getRealPath();
  55. Log::debug('文件类型'.$kuoname);
  56. //要保存的文件名 时间+扩展名
  57. if (in_array($kuoname, ['jpg', 'jpeg', 'gif', 'png'])) {
  58. $width = Image::make($filePath)->width();
  59. $height = Image::make($filePath)->height();
  60. //要保存的文件名 时间+扩展名
  61. $filename = time() . '_' . uniqid() . '.' . $kuoname;
  62. $fileurl = $filename . '?' . $width . '_' . $height;
  63. } else {
  64. //要保存的文件名 时间+扩展名
  65. $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname;
  66. }
  67. $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
  68. if ($imageUrl) {
  69. array_push($urls, config('customer.chxq_oss_url') . $path . $fileurl);
  70. } else {
  71. Log::debug('文件上传失败。' . $filename);
  72. }
  73. }
  74. if ($urls) {
  75. return $this->jsonSuccess($urls);
  76. } else {
  77. return $this->jsonError('图片上传失败,请重试');
  78. }
  79. }
  80. }