Explorar o código

oss图片上传

林琦海 %!s(int64=5) %!d(string=hai) anos
pai
achega
2ebf855679

+ 13 - 9
app/hd/controllers/UploadController.php

@@ -7,6 +7,7 @@ use bizHd\goods\services\PicCategoryService;
 use bizHd\goods\services\PicService;
 use common\components\business;
 use common\components\dirUtil;
+use common\components\oss;
 use common\components\stringUtil;
 use Yii;
 use common\components\util;
@@ -14,9 +15,9 @@ use yii\imagine\Image;
 
 class UploadController extends BaseController
 {
-	
+
 	public $guestAccessAction = [];
-	
+
 	//上传图片接口
 	public function actionSaveImg()
 	{
@@ -33,7 +34,7 @@ class UploadController extends BaseController
 				}
 			}
 		}
-		
+
 		//匹配出图片的格式
 		if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
 			$type = $result[2];
@@ -51,11 +52,11 @@ class UploadController extends BaseController
 			if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
 				util::fail('上传失败!!');
 			}
-			$img = "/uploads/{$merchantId}/{$month}/{$date}/" . $newFile;
+			$img = "uploads/{$merchantId}/{$month}/{$date}/" . $newFile;
 			$imgInfo = getimagesize($fullPathFile);
 			$width = $imgInfo[0];
 			$height = $imgInfo[1];
-			
+
 			//如果图片太大,进行压缩,并删除原图
 			if ($width > 800) {
 				$newWidth = 800;
@@ -66,11 +67,14 @@ class UploadController extends BaseController
 				Image::thumbnail($fullPathFile, $newWidth, $newHeight)->save($newFullPathFile, ['quality' => 100]);
 				$width = $newWidth;
 				$height = $newHeight;
-				$img = "/uploads/{$merchantId}/{$month}/{$date}/" . $newFile;
+				$img = "uploads/{$merchantId}/{$month}/{$date}/" . $newFile;
 				unlink($fullPathFile);
 				$fullPathFile = $newFullPathFile;
 			}
 
+			//上传oss  2021.3.18 linqh
+			oss::uploadImage($img,$fullPathFile);
+
 			//生成小图 200px
 			$smallWidth = 200;
 			$smallHeight = ceil((200 * $height) / $width);
@@ -96,7 +100,7 @@ class UploadController extends BaseController
 		}
 		util::fail('上传失败!');
 	}
-	
+
 	//保存图片 shish 2019.12.20
 	public function actionSave()
 	{
@@ -125,5 +129,5 @@ class UploadController extends BaseController
 			util::success($data);
 		}
 	}
-	
-}
+
+}

+ 10 - 7
common/components/fileUtil.php

@@ -6,7 +6,7 @@ use yii\imagine\Image;
 
 class fileUtil
 {
-	
+
 	//商家所用的图片上传 shish 2020.2.26
 	public static function imgUpload($merchantId)
 	{
@@ -33,7 +33,7 @@ class fileUtil
 			$imgInfo = getimagesize($fullPathFile);
 			$width = $imgInfo[0];
 			$height = $imgInfo[1];
-			
+
 			//如果图片太大,进行压缩,并删除原图
 			if ($width > 800) {
 				$newWidth = 800;
@@ -44,22 +44,25 @@ class fileUtil
 				Image::thumbnail($fullPathFile, $newWidth, $newHeight)->save($newFullPathFile, ['quality' => 100]);
 				$width = $newWidth;
 				$height = $newHeight;
-				$img = "/uploads/{$merchantId}/{$month}/{$date}/" . $newFile;
+				$img = "uploads/{$merchantId}/{$month}/{$date}/" . $newFile;
 				unlink($fullPathFile);
 				$fullPathFile = $newFullPathFile;
 			}
-			
+
+            //上传oss  2021.3.18 linqh
+            oss::uploadImage($img,$fullPathFile);
+
 			//生成小图 300px
 			$smallWidth = 300;
 			$smallHeight = ceil(($smallWidth * $height) / $width);
 			$smallFullPathFile = $path . $preFileName . '_small.' . $type;
 			Image::thumbnail($fullPathFile, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
-			
+
 			$data = business::formatUploadImg($img);
 			$data['shortUrl'] = $img;
 			return $data;
 		}
 		util::fail('上传失败!');
 	}
-	
-}
+
+}

+ 42 - 0
common/components/oss.php

@@ -0,0 +1,42 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/3/18 20:15
+ */
+
+namespace common\components;
+
+use OSS\Core\OssException;
+use OSS\OssClient;
+use Yii;
+class oss
+{
+
+    //上传文件
+    public static function uploadImage($object, $filePath)
+    {
+        $ossClient = self::getOssClient();
+        $bucket = Yii::$app->params['ossBucket'];
+        try{
+
+            $res = $ossClient->uploadFile($bucket, $object, $filePath);
+        }catch (OssException $e){
+            util::fail($e->getMessage());
+        }
+        return true;
+    }
+
+
+    public static function getOssClient()
+    {
+        $accessKeyId = Yii::$app->params['accessKeyId'];
+        $accessKeySecret = Yii::$app->params['accessKeySecret'];
+        $endpoint = Yii::$app->params['endpoint'];
+        try {
+            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
+            return $ossClient;
+        } catch (OssException $e) {
+            util::fail($e->getMessage());
+        }
+    }
+}

+ 19 - 7
common/config/params.php

@@ -39,6 +39,12 @@ $config = [
     //永不过期日期
     'neverExpireDate' => '2100-01-01 00:00:00',
     'pageSize' => 20,
+
+    //aliyun oss ak
+    'accessKeyId'=>'LTAI4GCXNRgt87qnCB8JrGFt',
+    'accessKeySecret'=>'m5q7aU3wGcoid9wr0yFJ4JqzHMKfks',
+    'endpoint'=>'oss-cn-hangzhou.aliyuncs.com',
+
 ];
 
 $prefixHttp = 'http://';
@@ -69,44 +75,50 @@ if (getenv('YII_ENV') == 'local') {
     $config['ghsPtHost'] = $prefixHttp . 'api.pt.xhb.com';
     $config['ghsShortHost'] = 'xhb.com';
     $config['ghsImgHost'] = $prefixHttp . 'img.xhb.com';
+    $config['ossBucket'] = 'pic-hhb-cs';
 }
 if (getenv('YII_ENV') == 'test') {
     $config['hdHost'] = $prefixHttp . 'api.shop.huaml.com';
     $config['hdDomain'] = $prefixHttp . 'shop.huaml.com';
     $config['hdPtHost'] = $prefixHttp . 'api.pt.huaml.com';
     $config['hdShortHost'] = 'huaml.com';
-    $config['hdImgHost'] = $prefixHttp . 'img.huaml.com';
+    $config['hdImgHost'] = $prefixHttp . 'img.huaml.com/';
 
     $config['ghsHost'] = $prefixHttp . 'api.shop.hzghd.com';
     $config['ghsDomain'] = $prefixHttp . 'shop.hzghd.com';
     $config['ghsPtHost'] = $prefixHttp . 'api.pt.hzghd.com';
     $config['ghsShortHost'] = 'hzghd.com';
-    $config['ghsImgHost'] = $prefixHttp . 'img.hzghd.com';
+    $config['ghsImgHost'] = $prefixHttp . 'img.hzghd.com/';
+    $config['ossBucket'] = 'pic-hhb-cs';
 }
 if (getenv('YII_ENV') == 'dev') {
     $config['hdHost'] = $prefixHttp . 'api.shop.huaml.com';
     $config['hdDomain'] = $prefixHttp . 'shop.huaml.com';
     $config['hdPtHost'] = $prefixHttp . 'api.pt.huaml.com';
     $config['hdShortHost'] = 'huaml.com';
-    $config['hdImgHost'] = $prefixHttp . 'img.huaml.com';
+    $config['hdImgHost'] = $prefixHttp . 'img.huaml.com/';
 
     $config['ghsHost'] = $prefixHttp . 'api.shop.hzghd.com';
     $config['ghsDomain'] = $prefixHttp . 'shop.hzghd.com';
     $config['ghsPtHost'] = $prefixHttp . 'api.pt.hzghd.com';
     $config['ghsShortHost'] = 'hzghd.com';
-    $config['ghsImgHost'] = $prefixHttp . 'img.hzghd.com';
+    $config['ghsImgHost'] = $prefixHttp . 'img.hzghd.com/';
+
+    $config['ossBucket'] = 'pic-hhb-cs';
 }
 if (getenv('YII_ENV') == 'production') {
     $config['hdHost'] = $prefixHttp . 'api.shop.huahb.com';
     $config['hdDomain'] = $prefixHttp . 'shop.huahb.com';
     $config['hdPtHost'] = $prefixHttp . 'api.pt.huahb.com';
     $config['hdShortHost'] = 'huahb.com';
-    $config['hdImgHost'] = $prefixHttp . 'img.huahb.com';
+    $config['hdImgHost'] = $prefixHttp . 'img.huahb.com/';
 
     $config['ghsHost'] = $prefixHttp . 'api.shop.huahb.cn';
     $config['ghsDomain'] = $prefixHttp . 'shop.huahb.cn';
     $config['ghsPtHost'] = $prefixHttp . 'api.pt.huahb.cn';
     $config['ghsShortHost'] = 'huahb.cn';
-    $config['ghsImgHost'] = $prefixHttp . 'img.huahb.cn';
+    $config['ghsImgHost'] = $prefixHttp . 'img.huahb.cn/';
+
+    $config['ossBucket'] = 'pic-hhb';
 }
-return $config;
+return $config;