123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- namespace App\Http\Controllers;
- use Acekyd\LaravelMP3\LaravelMP3;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- use Intervention\Image\Facades\Image;
- use Illuminate\Support\Facades\Validator;
- use Tymon\JWTAuth\Facades\JWTAuth;
- class UploadController extends Controller
- {
- public function uploadImage(Request $request)
- {
- if ($request->hasFile('image') && $request->file('image')->isValid()) {
- $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/';
- //获取文件的原文件名 包括扩展名
- // $yuanname= $request->file('image')->getClientOriginalName();
- // //获取文件的扩展名
- $kuoname = $request->file('image')->getClientOriginalExtension();
- // //获取文件的类型
- // $type=$request->file('image')->getClientMimeType();
- // //获取文件的绝对路径,但是获取到的在本地不能打开
- $filePath = $request->file('image')->getRealPath();
- //要保存的文件名 时间+扩展名
- if (in_array($kuoname, ['jpg', 'jpeg','png'])) {
- $width = Image::make($filePath)->width();
- $height = Image::make($filePath)->height();
- //要保存的文件名 时间+扩展名
- $filename = time() . '_' . uniqid() . '.' . $kuoname;
- $fileurl = $filename . '?' . $width . '_' . $height;
- } else {
- //要保存的文件名 时间+扩展名
- $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname;
- }
- $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
- if ($imageUrl) {
- return [
- 'data' => ['url' => $path . $fileurl]
- ];
- } else {
- return $this->response->error('图片上传失败,请重试', 500);
- }
- } else {
- return $this->response->error('图片上传失败,请重试', 500);
- }
- }
- public function uploadStream(Request $request)
- {
- if ($request->hasFile('image') && $request->file('image')->isValid()) {
- $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/';
- //获取文件的扩展名
- $kuoname = $request->file('image')->getClientOriginalExtension();
- //获取文件的绝对路径,但是获取到的在本地不能打开
- $filePath = $request->file('image')->getRealPath();
- //要保存的文件名 时间+扩展名
- $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname;
- $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
- if ($imageUrl) {
- return [
- 'data' => ['url' => $path . $fileurl]
- ];
- } else {
- return $this->response->error('上传失败,请重试', 500);
- }
- } else {
- return $this->response->error('上传失败,请重试', 500);
- }
- }
- public function uploadImages(Request $request)
- {
- $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/';
- $files = $request->file('image');
- $fileCount = count($files);
- $urls = [];
- for ($i = 0; $i < $fileCount; $i++) {
- $kuoname = $files[$i]->getClientOriginalExtension();
- $filePath = $files[$i]->getRealPath();
- //要保存的文件名 时间+扩展名
- if (in_array($kuoname, ['jpg', 'jpeg', 'png'])) {
- $width = Image::make($filePath)->width();
- $height = Image::make($filePath)->height();
- //要保存的文件名 时间+扩展名
- //$filename = time() . '_' . uniqid() . '*' . $width . '_' . $height . '.' . $kuoname;
- $filename = time() . '_' . uniqid() . '.' . $kuoname;
- $fileurl = time() . '_' . uniqid() . '.' . $kuoname . '?' . $width . '_' . $height;
- } else {
- //要保存的文件名 时间+扩展名
- $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname;
- }
- $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
- if ($imageUrl) {
- array_push($urls, $path . $fileurl);
- } else {
- Log::debug('文件上传失败。' . $filename);
- }
- }
- if ($urls) {
- return [
- 'data' => $urls
- ];
- } else {
- return $this->response->error('图片上传失败,请重试', 500);
- }
- }
- public function uploadMp3(Request $request)
- {
- if ($request->hasFile('mp3') && $request->file('mp3')->isValid()) {
- $path = 'music' . '/';
- //获取文件的扩展名
- $kuoname = $request->file('mp3')->getClientOriginalExtension();
- //获取文件的绝对路径,但是获取到的在本地不能打开
- $filePath = $request->file('mp3')->getRealPath();
- //计算音乐时长
- $laravel_mp3 = new LaravelMP3();
- $details = $laravel_mp3->getDuration($filePath);
- Log::debug('文件类型' . $kuoname);
- //要保存的文件名 时间+扩展名
- $filename = time() . '_' . uniqid() . '*' . $kuoname;
- $mp3Url = Storage::put($path . $filename, file_get_contents($filePath));
- Log::debug('upload_url:' . $mp3Url);
- if ($mp3Url) {
- $data = ['url' => config('customer.chxq_oss_url') . $path . $filename, 'music_duration' => $details];
- return $data;
- } else {
- return 'mp3上传失败,请重试';
- }
- } else {
- return '仅支持mp3上传,请重试';
- }
- }
- /**
- * 处理批量上传
- */
- public function uploadImagesChange(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'imgs' => 'required_if:type,image|array|max:9',
- 'imgs.*' => 'required|url',
- ]);
- if ($validator->fails()) {
- return $this->response->error($validator->errors()->first(), 500);
- }
- $data = [];
- foreach($request['imgs'] as $img){
- $res = $this->uploadOneImage($img);
- if(!isset($res['data']['url'])){
- return $this->response->error('上传失败,请重试', 500);
- }
- $data[] = config('customer.chxq_oss_url').$res['data']['url'];
- }
- return [
- 'data' => $data
- ];
- }
- public function uploadOneImage($img) {
- $path = public_path('image');
- if (!file_exists($path)){
- mkdir ($path,0777,true);
- }
- $fileUrl = $path.date('/Ymd').time(). uniqid() .'.jpg';
- $content = file_get_contents($img);
- file_put_contents($fileUrl, $content);
- try {
- $url = config("customer.manage_service_url").'/config/upload';
- $array = [
- 'multipart' => [
- [
- 'name' => 'image',
- 'contents' => fopen($fileUrl, 'r'),
- 'headers' => ['X-Baz' => 'bar'],
- 'filename' => $fileUrl
- ]
- ],
- 'query' => [],
- 'http_errors' => false,
- 'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
- ];
- $client = new \GuzzleHttp\Client();
- $response = $client->request('post', $url, $array);
- $result = json_decode($response->getBody()->getContents(), true);
- } catch (\Exception $e) {
- $result = '';
- }
- unlink($fileUrl);
- return $result;
- }
- }
|