UploadController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\goods\services\CategoryService;
  4. use common\components\business;
  5. use common\components\dirUtil;
  6. use common\components\oss;
  7. use common\components\stringUtil;
  8. use common\components\noticeUtil;
  9. use Yii;
  10. use common\components\util;
  11. use yii\imagine\Image;
  12. class UploadController extends BaseController
  13. {
  14. public $guestAccess = ['save-img', 'save-file'];
  15. //保存图片上传
  16. public function actionSaveFile()
  17. {
  18. $uploadPath = 'uploads';
  19. // 对上传的文件进行多文件夹管理
  20. $rootPath = Yii::$app->request->post('rootPath');
  21. if (!empty($rootPath) && in_array($rootPath, oss::ROOT_PATHS)) {
  22. $uploadPath = $rootPath;
  23. }
  24. $file = $_FILES['file'];
  25. if (!is_uploaded_file($file['tmp_name'])) {
  26. util::fail('please upload img');
  27. }
  28. $mainId = $this->mainId; // 变动:$this->sjId 改为 $this->mainId
  29. if (intval($mainId) == 0) {
  30. Yii::error('------- lose token -------');
  31. noticeUtil::push('save-file 无token进行上传文件');
  32. }
  33. $shopId = $this->shopId;
  34. $month = date("Ym");
  35. $date = date("d");
  36. $path = dirUtil::getImgUploadDir() . "/{$mainId}/{$shopId}/{$month}/{$date}/";
  37. if (!file_exists($path)) {
  38. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  39. mkdir($path, 0700, true);
  40. }
  41. $preFileName = stringUtil::uniqueFileName();
  42. $newFile = $preFileName . ".jpg";
  43. $fullPathFile = $path . $newFile;
  44. if (move_uploaded_file($file['tmp_name'], $fullPathFile)) {
  45. $img = "{$uploadPath}/{$mainId}/{$shopId}/{$month}/{$date}/" . $newFile;
  46. oss::uploadImage($img, $fullPathFile);
  47. $data = business::formatUploadImg($img);
  48. $data['shortUrl'] = $img;
  49. $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
  50. util::success($data, 'ok');
  51. } else {
  52. util::fail('fail');
  53. }
  54. }
  55. //上传图片接口
  56. public function actionSaveImg()
  57. {
  58. header('Content-type:text/html;charset=utf-8');
  59. $base64 = file_get_contents('php://input');
  60. $base64_image_content = str_replace("content=", "", urldecode($base64));
  61. $categoryIds = Yii::$app->request->post('categoryId', []);
  62. if (!empty($categoryIds)) {
  63. $categoryList = CategoryService::getByIds($categoryIds);
  64. foreach ($categoryList as $cat) {
  65. if ($cat['sjId'] != $this->sjId) {
  66. util::fail('请选择自己的分类');
  67. }
  68. }
  69. }
  70. //匹配出图片的格式
  71. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  72. $type = $result[2];
  73. $sjId = $this->sjId;
  74. $month = date("Ym");
  75. $date = date("d");
  76. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  77. if (!file_exists($path)) {
  78. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  79. mkdir($path, 0700, true);
  80. }
  81. $preFileName = stringUtil::uniqueFileName();
  82. $newFile = $preFileName . ".{$type}";
  83. $fullPathFile = $path . $newFile;
  84. if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  85. util::fail('上传失败!!');
  86. }
  87. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  88. $imgInfo = getimagesize($fullPathFile);
  89. $width = $imgInfo[0];
  90. $height = $imgInfo[1];
  91. //如果图片太大,进行压缩,并删除原图
  92. if ($width > 800) {
  93. $newWidth = 800;
  94. $newHeight = ceil((800 * $height) / $width);
  95. $preFileName = stringUtil::uniqueFileName();
  96. $newFile = $preFileName . ".{$type}";
  97. $newFullPathFile = $path . $newFile;
  98. Image::thumbnail($fullPathFile, $newWidth, $newHeight)->save($newFullPathFile, ['quality' => 100]);
  99. $width = $newWidth;
  100. $height = $newHeight;
  101. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  102. unlink($fullPathFile);
  103. $fullPathFile = $newFullPathFile;
  104. }
  105. //上传oss 2021.3.18 lqh
  106. oss::uploadImage($img, $fullPathFile);
  107. //生成小图 200px
  108. //$smallWidth = 200;
  109. //$smallHeight = ceil((200 * $height) / $width);
  110. //$smallFullPathFile = $path . $preFileName . '_small.' . $type;
  111. //Image::thumbnail($fullPathFile, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
  112. $data = business::formatUploadImg($img);
  113. // if (!empty($categoryIds)) {
  114. // $time = time();
  115. // $data = ['img' => $img, 'sjId' => $sjId, 'name' => '未命名', 'addTime' => $time];
  116. // $pic = PicService::add($data);
  117. // $currentId = $pic['id'];
  118. // $add = [];
  119. // foreach ($categoryIds as $cat) {
  120. // $add[] = ['categoryId' => $cat, 'picId' => $currentId, 'sjId' => $this->sjId];
  121. // }
  122. // PicCategoryService::batchAdd($add);
  123. // }
  124. $data['shortUrl'] = $img;
  125. $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
  126. util::success($data);
  127. }
  128. util::fail('上传失败!');
  129. }
  130. //保存图片 ssh 2019.12.20
  131. public function actionSave()
  132. {
  133. header('Content-type:text/html;charset=utf-8');
  134. $base64_image_content = '';
  135. //匹配出图片的格式
  136. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  137. $type = $result[2];
  138. $sjId = 12358;
  139. $month = date("Ym");
  140. $date = date("d");
  141. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  142. if (!file_exists($path)) {
  143. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  144. mkdir($path, 0700, true);
  145. }
  146. $preFileName = stringUtil::uniqueFileName();
  147. $newFile = $preFileName . ".{$type}";
  148. $fullPath = $path . $newFile;
  149. if (file_put_contents($fullPath, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  150. util::fail('上传失败');
  151. }
  152. $img = "/uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  153. $data = business::formatUploadImg($img);
  154. $data['shortUrl'] = $img;
  155. $data['smallShortUrl'] = $img . "?x-oss-process=image/resize,l_200";
  156. util::success($data);
  157. }
  158. }
  159. //删除oss上的图片
  160. public function actionDeleteFile()
  161. {
  162. $filePath = Yii::$app->request->post('filePath');
  163. if(empty($filePath)){
  164. util::fail('参数错误');
  165. }
  166. // 删除前判断文件是否存在
  167. if(!oss::fileExist($filePath)){
  168. Yii::error('file not exist: ' . $filePath);
  169. util::fail('file not exist');
  170. } else {
  171. Yii::info('file exist: ' . $filePath);
  172. }
  173. // 删除OSS文件
  174. try {
  175. // 设置超时时间为15秒
  176. set_time_limit(15);
  177. $startTime = time();
  178. $timeout = 15; // 15秒超时
  179. oss::deleteObject($filePath);
  180. // 检查是否超时
  181. if (time() - $startTime > $timeout) {
  182. throw new \Exception('删除文件操作超时');
  183. }
  184. } catch (\Exception $e) {
  185. util::fail('删除文件失败:' . $e->getMessage());
  186. }
  187. util::success('success');
  188. }
  189. //批量删除oss上的图片
  190. public function actionDeleteFiles()
  191. {
  192. $filePaths = Yii::$app->request->post('filePaths');
  193. if(empty($filePaths) || !is_array($filePaths)){
  194. util::fail('参数错误');
  195. }
  196. foreach($filePaths as $filePath){
  197. if(!oss::fileExist($filePath)){
  198. Yii::error('file not exist: ' . $filePath);
  199. util::fail('file not exist');
  200. } else {
  201. Yii::info('file exist: ' . $filePath);
  202. }
  203. }
  204. // 删除OSS文件
  205. try {
  206. oss::deleteObjects($filePaths);
  207. } catch (\Exception $e) {
  208. util::fail('删除文件失败:' . $e->getMessage());
  209. }
  210. util::success('success');
  211. }
  212. }