UploadController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace ghs\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\stringUtil;
  9. use Yii;
  10. use common\components\util;
  11. use yii\imagine\Image;
  12. use common\components\oss;
  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::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. util::success($data);
  146. }
  147. }
  148. //上传图片--小菊
  149. public function actionNewSave()
  150. {
  151. header('Content-type:text/html;charset=utf-8');
  152. $base64 = file_get_contents('php://input');
  153. $base64_image_content = str_replace("content=", "", urldecode($base64));
  154. //匹配出图片的格式
  155. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  156. $type = $result[2];
  157. $sjId = 0;
  158. $month = date("Ym");
  159. $date = date("d");
  160. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  161. if (!file_exists($path)) {
  162. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  163. mkdir($path, 0700, true);
  164. }
  165. $preFileName = stringUtil::uniqueFileName();
  166. $newFile = $preFileName . ".{$type}";
  167. $fullPathFile = $path . $newFile;
  168. if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  169. util::fail('上传失败!!');
  170. }
  171. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  172. $imgInfo = getimagesize($fullPathFile);
  173. $width = $imgInfo[0];
  174. $height = $imgInfo[1];
  175. //如果图片太大,进行压缩,并删除原图
  176. if ($width > 800) {
  177. $newWidth = 800;
  178. $newHeight = ceil((800 * $height) / $width);
  179. $preFileName = stringUtil::uniqueFileName();
  180. $newFile = $preFileName . ".{$type}";
  181. $newFullPathFile = $path . $newFile;
  182. Image::thumbnail($fullPathFile, $newWidth, $newHeight)->save($newFullPathFile, ['quality' => 100]);
  183. $width = $newWidth;
  184. $height = $newHeight;
  185. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  186. unlink($fullPathFile);
  187. $fullPathFile = $newFullPathFile;
  188. }
  189. //上传oss 2021.3.18 lqh
  190. oss::uploadImage($img, $fullPathFile);
  191. //生成小图 200px
  192. //$smallWidth = 200;
  193. //$smallHeight = ceil((200 * $height) / $width);
  194. //$smallFullPathFile = $path . $preFileName . '_small.' . $type;
  195. //Image::thumbnail($fullPathFile, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
  196. $data = business::formatUploadImg($img);
  197. $data['shortUrl'] = $img;
  198. util::success($data);
  199. }
  200. util::fail('上传失败!');
  201. }
  202. }