UploadController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Acekyd\LaravelMP3\LaravelMP3;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\Storage;
  7. use Intervention\Image\Facades\Image;
  8. use Illuminate\Support\Facades\Validator;
  9. use Tymon\JWTAuth\Facades\JWTAuth;
  10. class UploadController extends Controller
  11. {
  12. public function uploadImage(Request $request)
  13. {
  14. if ($request->hasFile('image') && $request->file('image')->isValid()) {
  15. $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/';
  16. //获取文件的原文件名 包括扩展名
  17. // $yuanname= $request->file('image')->getClientOriginalName();
  18. // //获取文件的扩展名
  19. $kuoname = $request->file('image')->getClientOriginalExtension();
  20. // //获取文件的类型
  21. // $type=$request->file('image')->getClientMimeType();
  22. // //获取文件的绝对路径,但是获取到的在本地不能打开
  23. $filePath = $request->file('image')->getRealPath();
  24. //要保存的文件名 时间+扩展名
  25. if (in_array($kuoname, ['jpg', 'jpeg','png'])) {
  26. $width = Image::make($filePath)->width();
  27. $height = Image::make($filePath)->height();
  28. //要保存的文件名 时间+扩展名
  29. $filename = time() . '_' . uniqid() . '.' . $kuoname;
  30. $fileurl = $filename . '?' . $width . '_' . $height;
  31. } else {
  32. //要保存的文件名 时间+扩展名
  33. $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname;
  34. }
  35. $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
  36. if ($imageUrl) {
  37. return [
  38. 'data' => ['url' => $path . $fileurl]
  39. ];
  40. } else {
  41. return $this->response->error('图片上传失败,请重试', 500);
  42. }
  43. } else {
  44. return $this->response->error('图片上传失败,请重试', 500);
  45. }
  46. }
  47. public function uploadStream(Request $request)
  48. {
  49. if ($request->hasFile('image') && $request->file('image')->isValid()) {
  50. $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/';
  51. //获取文件的扩展名
  52. $kuoname = $request->file('image')->getClientOriginalExtension();
  53. //获取文件的绝对路径,但是获取到的在本地不能打开
  54. $filePath = $request->file('image')->getRealPath();
  55. //要保存的文件名 时间+扩展名
  56. $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname;
  57. $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
  58. if ($imageUrl) {
  59. return [
  60. 'data' => ['url' => $path . $fileurl]
  61. ];
  62. } else {
  63. return $this->response->error('上传失败,请重试', 500);
  64. }
  65. } else {
  66. return $this->response->error('上传失败,请重试', 500);
  67. }
  68. }
  69. public function uploadImages(Request $request)
  70. {
  71. $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/';
  72. $files = $request->file('image');
  73. $fileCount = count($files);
  74. $urls = [];
  75. for ($i = 0; $i < $fileCount; $i++) {
  76. $kuoname = $files[$i]->getClientOriginalExtension();
  77. $filePath = $files[$i]->getRealPath();
  78. //要保存的文件名 时间+扩展名
  79. if (in_array($kuoname, ['jpg', 'jpeg', 'png'])) {
  80. $width = Image::make($filePath)->width();
  81. $height = Image::make($filePath)->height();
  82. //要保存的文件名 时间+扩展名
  83. //$filename = time() . '_' . uniqid() . '*' . $width . '_' . $height . '.' . $kuoname;
  84. $filename = time() . '_' . uniqid() . '.' . $kuoname;
  85. $fileurl = time() . '_' . uniqid() . '.' . $kuoname . '?' . $width . '_' . $height;
  86. } else {
  87. //要保存的文件名 时间+扩展名
  88. $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname;
  89. }
  90. $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
  91. if ($imageUrl) {
  92. array_push($urls, $path . $fileurl);
  93. } else {
  94. Log::debug('文件上传失败。' . $filename);
  95. }
  96. }
  97. if ($urls) {
  98. return [
  99. 'data' => $urls
  100. ];
  101. } else {
  102. return $this->response->error('图片上传失败,请重试', 500);
  103. }
  104. }
  105. public function uploadMp3(Request $request)
  106. {
  107. if ($request->hasFile('mp3') && $request->file('mp3')->isValid()) {
  108. $path = 'music' . '/';
  109. //获取文件的扩展名
  110. $kuoname = $request->file('mp3')->getClientOriginalExtension();
  111. //获取文件的绝对路径,但是获取到的在本地不能打开
  112. $filePath = $request->file('mp3')->getRealPath();
  113. //计算音乐时长
  114. $laravel_mp3 = new LaravelMP3();
  115. $details = $laravel_mp3->getDuration($filePath);
  116. Log::debug('文件类型' . $kuoname);
  117. //要保存的文件名 时间+扩展名
  118. $filename = time() . '_' . uniqid() . '*' . $kuoname;
  119. $mp3Url = Storage::put($path . $filename, file_get_contents($filePath));
  120. Log::debug('upload_url:' . $mp3Url);
  121. if ($mp3Url) {
  122. $data = ['url' => config('customer.chxq_oss_url') . $path . $filename, 'music_duration' => $details];
  123. return $data;
  124. } else {
  125. return 'mp3上传失败,请重试';
  126. }
  127. } else {
  128. return '仅支持mp3上传,请重试';
  129. }
  130. }
  131. /**
  132. * 处理批量上传
  133. */
  134. public function uploadImagesChange(Request $request)
  135. {
  136. $validator = Validator::make($request->all(), [
  137. 'imgs' => 'required_if:type,image|array|max:9',
  138. 'imgs.*' => 'required|url',
  139. ]);
  140. if ($validator->fails()) {
  141. return $this->response->error($validator->errors()->first(), 500);
  142. }
  143. $data = [];
  144. foreach($request['imgs'] as $img){
  145. $res = $this->uploadOneImage($img);
  146. if(!isset($res['data']['url'])){
  147. return $this->response->error('上传失败,请重试', 500);
  148. }
  149. $data[] = config('customer.chxq_oss_url').$res['data']['url'];
  150. }
  151. return [
  152. 'data' => $data
  153. ];
  154. }
  155. public function uploadOneImage($img) {
  156. $path = public_path('image');
  157. if (!file_exists($path)){
  158. mkdir ($path,0777,true);
  159. }
  160. $fileUrl = $path.date('/Ymd').time(). uniqid() .'.jpg';
  161. $content = file_get_contents($img);
  162. file_put_contents($fileUrl, $content);
  163. try {
  164. $url = config("customer.manage_service_url").'/config/upload';
  165. $array = [
  166. 'multipart' => [
  167. [
  168. 'name' => 'image',
  169. 'contents' => fopen($fileUrl, 'r'),
  170. 'headers' => ['X-Baz' => 'bar'],
  171. 'filename' => $fileUrl
  172. ]
  173. ],
  174. 'query' => [],
  175. 'http_errors' => false,
  176. 'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
  177. ];
  178. $client = new \GuzzleHttp\Client();
  179. $response = $client->request('post', $url, $array);
  180. $result = json_decode($response->getBody()->getContents(), true);
  181. } catch (\Exception $e) {
  182. $result = '';
  183. }
  184. unlink($fileUrl);
  185. return $result;
  186. }
  187. }