UploadController.php 4.2 KB

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