shish 6 anni fa
parent
commit
9c8a724246

+ 5 - 4
app/business/controllers/PicController.php

@@ -5,6 +5,7 @@ namespace business\controllers;
 use biz\goods\services\CategoryService;
 use biz\goods\services\PicCategoryService;
 use biz\goods\services\PicService;
+use common\components\fileUtil;
 use Yii;
 use common\components\util;
 
@@ -28,15 +29,15 @@ class PicController extends BaseController
 	//上传图片 shish 2019.12.7
 	public function actionAdd()
 	{
-		$img = '/uploads/201905/25/jpg/19052591518_12358.jpg';
-		$prefix = Yii::$app->params['imgHost'];
-		$url = $prefix . $img;
+		$return = fileUtil::imgUpload($this->merchantId);
 		$merchantId = $this->merchantId;
 		$time = time();
 		$name = '未命名';
+		$img = isset($return['shortUrl']) ? $return['shortUrl'] : '';
 		$data = ['img' => $img, 'merchantId' => $merchantId, 'name' => $name, 'addTime' => $time];
 		$pic = PicService::add($data);
-		util::success(['id' => $pic['id'], 'url' => $url, 'shortUrl' => $img]);
+		$return['id'] = $pic['id'];
+		util::success($return);
 	}
 	
 	//备注图片 shish 2019.12.8

+ 2 - 2
biz/goods/services/PicCategoryService.php

@@ -8,9 +8,9 @@ use biz\goods\classes\PicClass;
 
 class PicCategoryService extends BaseService
 {
-	
+
 	public static $baseFile = '\biz\goods\classes\PicCategoryClass';
-	
+
 	//图库列表 shish 2019.12.7
 	public static function getPicList($where)
 	{

+ 65 - 0
common/components/fileUtil.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace common\components;
+
+use yii\imagine\Image;
+
+class fileUtil
+{
+	
+	//商家所用的图片上传 shish 2020.2.26
+	public static function imgUpload($merchantId)
+	{
+		header('Content-type:text/html;charset=utf-8');
+		$base64 = file_get_contents('php://input');
+		$base64_image_content = str_replace("content=", "", urldecode($base64));
+		//匹配出图片的格式
+		if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
+			$type = $result[2];
+			$month = date("Ym");
+			$date = date("d");
+			$path = dirUtil::getImgUploadDir() . "/{$merchantId}/{$month}/{$date}/";
+			if (!file_exists($path)) {
+				//检查是否有该文件夹,如果没有就创建,并给予最高权限
+				mkdir($path, 0700, true);
+			}
+			$preFileName = stringUtil::uniqueFileName();
+			$newFile = $preFileName . ".{$type}";
+			$fullPathFile = $path . $newFile;
+			if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
+				util::fail('上传失败!!');
+			}
+			$img = "/uploads/{$merchantId}/{$month}/{$date}/" . $newFile;
+			$imgInfo = getimagesize($fullPathFile);
+			$width = $imgInfo[0];
+			$height = $imgInfo[1];
+			
+			//如果图片太大,进行压缩,并删除原图
+			if ($width > 800) {
+				$newWidth = 800;
+				$newHeight = ceil((800 * $height) / $width);
+				$preFileName = stringUtil::uniqueFileName();
+				$newFile = $preFileName . ".{$type}";
+				$newFullPathFile = $path . $newFile;
+				Image::thumbnail($fullPathFile, $newWidth, $newHeight)->save($newFullPathFile, ['quality' => 100]);
+				$width = $newWidth;
+				$height = $newHeight;
+				$img = "/uploads/{$merchantId}/{$month}/{$date}/" . $newFile;
+				unlink($fullPathFile);
+				$fullPathFile = $newFullPathFile;
+			}
+			
+			//生成小图 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('上传失败!');
+	}
+	
+}