UploadController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\goods\services\CategoryService;
  4. use bizHd\goods\services\PicCategoryService;
  5. use bizHd\goods\services\PicService;
  6. use common\components\business;
  7. use common\components\dirUtil;
  8. use common\components\oss;
  9. use common\components\stringUtil;
  10. use Yii;
  11. use common\components\util;
  12. use yii\imagine\Image;
  13. class UploadController extends BaseController
  14. {
  15. public $guestAccess = ['save-img', 'save-file'];
  16. //保存图片上传
  17. public function actionSaveFile()
  18. {
  19. $file = $_FILES['file'];
  20. if (!is_uploaded_file($file['tmp_name'])) {
  21. util::fail('please upload img');
  22. }
  23. $mainId = $this->sjId;
  24. $shopId = $this->shopId;
  25. $month = date("Ym");
  26. $date = date("d");
  27. $path = dirUtil::getImgUploadDir() . "/{$mainId}/{$shopId}/{$month}/{$date}/";
  28. if (!file_exists($path)) {
  29. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  30. mkdir($path, 0700, true);
  31. }
  32. $preFileName = stringUtil::uniqueFileName();
  33. $newFile = $preFileName . ".jpg";
  34. $fullPathFile = $path . $newFile;
  35. if (move_uploaded_file($file['tmp_name'], $fullPathFile)) {
  36. $img = "uploads/{$mainId}/{$shopId}/{$month}/{$date}/" . $newFile;
  37. oss::uploadImage($img, $fullPathFile);
  38. $data = business::formatUploadImg($img);
  39. $data['shortUrl'] = $img;
  40. $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
  41. util::success($data, 'ok');
  42. } else {
  43. util::fail('fail');
  44. }
  45. }
  46. //上传图片接口
  47. public function actionSaveImg()
  48. {
  49. header('Content-type:text/html;charset=utf-8');
  50. $base64 = file_get_contents('php://input');
  51. $base64_image_content = str_replace("content=", "", urldecode($base64));
  52. $categoryIds = Yii::$app->request->post('categoryId', []);
  53. if (!empty($categoryIds)) {
  54. $categoryList = CategoryService::getByIds($categoryIds);
  55. foreach ($categoryList as $cat) {
  56. if ($cat['sjId'] != $this->sjId) {
  57. util::fail('请选择自己的分类');
  58. }
  59. }
  60. }
  61. //匹配出图片的格式
  62. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  63. $type = $result[2];
  64. $sjId = $this->sjId;
  65. $month = date("Ym");
  66. $date = date("d");
  67. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  68. if (!file_exists($path)) {
  69. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  70. mkdir($path, 0700, true);
  71. }
  72. $preFileName = stringUtil::uniqueFileName();
  73. $newFile = $preFileName . ".{$type}";
  74. $fullPathFile = $path . $newFile;
  75. if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  76. util::fail('上传失败!!');
  77. }
  78. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  79. $imgInfo = getimagesize($fullPathFile);
  80. $width = $imgInfo[0];
  81. $height = $imgInfo[1];
  82. //如果图片太大,进行压缩,并删除原图
  83. if ($width > 800) {
  84. $newWidth = 800;
  85. $newHeight = ceil((800 * $height) / $width);
  86. $preFileName = stringUtil::uniqueFileName();
  87. $newFile = $preFileName . ".{$type}";
  88. $newFullPathFile = $path . $newFile;
  89. Image::thumbnail($fullPathFile, $newWidth, $newHeight)->save($newFullPathFile, ['quality' => 100]);
  90. $width = $newWidth;
  91. $height = $newHeight;
  92. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  93. unlink($fullPathFile);
  94. $fullPathFile = $newFullPathFile;
  95. }
  96. //上传oss 2021.3.18 lqh
  97. oss::uploadImage($img, $fullPathFile);
  98. //生成小图 200px
  99. //$smallWidth = 200;
  100. //$smallHeight = ceil((200 * $height) / $width);
  101. //$smallFullPathFile = $path . $preFileName . '_small.' . $type;
  102. //Image::thumbnail($fullPathFile, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
  103. $data = business::formatUploadImg($img);
  104. // if (!empty($categoryIds)) {
  105. // $time = time();
  106. // $data = ['img' => $img, 'sjId' => $sjId, 'name' => '未命名', 'addTime' => $time];
  107. // $pic = PicService::add($data);
  108. // $currentId = $pic['id'];
  109. // $add = [];
  110. // foreach ($categoryIds as $cat) {
  111. // $add[] = ['categoryId' => $cat, 'picId' => $currentId, 'sjId' => $this->sjId];
  112. // }
  113. // PicCategoryService::batchAdd($add);
  114. // }
  115. $data['shortUrl'] = $img;
  116. $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
  117. util::success($data);
  118. }
  119. util::fail('上传失败!');
  120. }
  121. //保存图片 ssh 2019.12.20
  122. public function actionSave()
  123. {
  124. header('Content-type:text/html;charset=utf-8');
  125. $base64_image_content = '';
  126. //匹配出图片的格式
  127. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  128. $type = $result[2];
  129. $sjId = 12358;
  130. $month = date("Ym");
  131. $date = date("d");
  132. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  133. if (!file_exists($path)) {
  134. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  135. mkdir($path, 0700, true);
  136. }
  137. $preFileName = stringUtil::uniqueFileName();
  138. $newFile = $preFileName . ".{$type}";
  139. $fullPath = $path . $newFile;
  140. if (file_put_contents($fullPath, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  141. util::fail('上传失败');
  142. }
  143. $img = "/uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  144. $data = business::formatUploadImg($img);
  145. $data['shortUrl'] = $img;
  146. $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
  147. util::success($data);
  148. }
  149. }
  150. }