UploadController.php 3.4 KB

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