UploadController.php 4.2 KB

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