xielin 5 år sedan
förälder
incheckning
406cdba8fa
3 ändrade filer med 28 tillägg och 8 borttagningar
  1. 24 6
      app/Http/Controllers/UploadController.php
  2. 2 1
      bootstrap/app.php
  3. 2 1
      composer.json

+ 24 - 6
app/Http/Controllers/UploadController.php

@@ -5,24 +5,34 @@ namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Storage;
+use Intervention\Image\Facades\Image;
 
 class UploadController extends Controller
 {
-    public function uploadImage(Request $request){
+    public function uploadImage(Request $request)
+    {
         if ($request->hasFile('image') && $request->file('image')->isValid()) {
             $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/';
 //            //获取文件的扩展名
             $kuoname = $request->file('image')->getClientOriginalExtension();
+
 //            //获取文件的类型
 //            $type=$request->file('image')->getClientMimeType();
 //            //获取文件的绝对路径,但是获取到的在本地不能打开
             $filePath = $request->file('image')->getRealPath();
-            //要保存的文件名 时间+扩展名
-            $filename = time() . '_' . uniqid() . '.' . $kuoname;
+            if (in_array($kuoname, ['jpg', 'jpeg', 'png'])) {
+                $width = Image::make($filePath)->width();
+                $height = Image::make($filePath)->height();
+                //要保存的文件名 时间+扩展名
+                $filename = time() . '_' . uniqid() . '*' . $width . '_' . $height . '.' . $kuoname;
+            } else {
+                //要保存的文件名 时间+扩展名
+                $filename = time() . '_' . uniqid() . '.' . $kuoname;
+            }
             $imageUrl = Storage::put($path . $filename, file_get_contents($filePath));
             Log::debug('upload_url:' . $imageUrl);
             if ($imageUrl) {
-                return $this->jsonSuccess(['url' => config('customer.chxq_oss_url').$path . $filename]);
+                return $this->jsonSuccess(['url' => config('customer.chxq_oss_url') . $path . $filename]);
             } else {
                 return $this->jsonError('图片上传失败,请重试');
             }
@@ -40,10 +50,18 @@ class UploadController extends Controller
             $kuoname = $files[$i]->getClientOriginalExtension();
             $filePath = $files[$i]->getRealPath();
             //要保存的文件名 时间+扩展名
-            $filename = time() . '_' . uniqid() . '.' . $kuoname;
+            if (in_array($kuoname, ['jpg', 'jpeg', 'png'])) {
+                $width = Image::make($filePath)->width();
+                $height = Image::make($filePath)->height();
+                //要保存的文件名 时间+扩展名
+                $filename = time() . '_' . uniqid() . '*' . $width . '_' . $height . '.' . $kuoname;
+            } else {
+                //要保存的文件名 时间+扩展名
+                $filename = time() . '_' . uniqid() . '.' . $kuoname;
+            }
             $imageUrl = Storage::put($filename, file_get_contents($filePath));
             if ($imageUrl) {
-                array_push($urls, config('customer.chxq_oss_url').$filename);
+                array_push($urls, config('customer.chxq_oss_url') . $filename);
             } else {
                 Log::debug('文件上传失败。' . $filename);
             }

+ 2 - 1
bootstrap/app.php

@@ -27,9 +27,9 @@ $app->withEloquent();
 
 $app->configure('api');
 $app->configure('apollo');
+$app->configure('customer');
 $app->configure('auth');
 $app->configure('jwt');
-$app->configure('customer');
 $app->configure('database');
 /*
 |--------------------------------------------------------------------------
@@ -88,6 +88,7 @@ $app->register(Dingo\Api\Provider\LumenServiceProvider::class);
 $app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
 $app->register(\Junliuxian\AliOSS\AliOssServiceProvider::class);
 $app->register(\Illuminate\Redis\RedisServiceProvider::class);
+$app->register(\Intervention\Image\ImageServiceProvider::class);
 
 /*
 |--------------------------------------------------------------------------

+ 2 - 1
composer.json

@@ -10,13 +10,14 @@
         "doctrine/dbal": "^2.9",
         "hhxsv5/laravel-s": "~3.4.0",
         "illuminate/redis": "^5.8",
+        "intervention/image": "^2.4",
         "jeroendesloovere/distance": "1.0.2.1",
         "junliuxian/ali-oss-storage": "~2.0",
         "laravel/lumen-framework": "5.8.*",
         "league/fractal": "^0.17.0",
+        "multilinguals/apollo-client": "^0.1.2",
         "predis/predis": "^1.1",
         "tymon/jwt-auth": "1.0.0-rc.4.1",
-        "multilinguals/apollo-client": "^0.1.2",
         "vlucas/phpdotenv": "^3.3"
     },
     "require-dev": {