UploadController.php 6.3 KB

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