UploadController.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // //获取文件的扩展名
  14. $kuoname = $request->file('image')->getClientOriginalExtension();
  15. // //获取文件的类型
  16. // $type=$request->file('image')->getClientMimeType();
  17. // //获取文件的绝对路径,但是获取到的在本地不能打开
  18. $filePath = $request->file('image')->getRealPath();
  19. Log::debug('文件类型'.$kuoname);
  20. if (in_array($kuoname, ['jpg', 'jpeg', 'gif', 'png'])) {
  21. $width = Image::make($filePath)->width();
  22. $height = Image::make($filePath)->height();
  23. //要保存的文件名 时间+扩展名
  24. $filename = time() . '_' . uniqid() . '*' . $width . '_' . $height . '.' . $kuoname;
  25. //todo 等待客户端更新后替换为下列规则
  26. //$filename = time() . '_' . uniqid() . '.' . $kuoname . '?' . $width . '_' . $height;
  27. } else {
  28. //要保存的文件名 时间+扩展名
  29. $filename = time() . '_' . uniqid() . '.' . $kuoname;
  30. }
  31. $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
  32. Log::debug('upload_url:' . $imageUrl);
  33. if ($imageUrl) {
  34. return $this->jsonSuccess(['url' => config('customer.chxq_oss_url') . $path . $filename]);
  35. } else {
  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() . '*' . $width . '_' . $height . '.' . $kuoname;
  58. } else {
  59. //要保存的文件名 时间+扩展名
  60. $filename = time() . '_' . uniqid() . '.' . $kuoname;
  61. }
  62. $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
  63. if ($imageUrl) {
  64. array_push($urls, config('customer.chxq_oss_url') . $path . $filename);
  65. } else {
  66. Log::debug('文件上传失败。' . $filename);
  67. }
  68. }
  69. if ($urls) {
  70. return $this->jsonSuccess($urls);
  71. } else {
  72. return $this->jsonError('图片上传失败,请重试');
  73. }
  74. }
  75. }