UploadController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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'];
  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['sjId'] != $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. $sjId = $this->sjId;
  35. $month = date("Ym");
  36. $date = date("d");
  37. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$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/{$sjId}/{$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/{$sjId}/{$month}/{$date}/" . $newFile;
  63. unlink($fullPathFile);
  64. $fullPathFile = $newFullPathFile;
  65. }
  66. oss::uploadImage($img,$fullPathFile);
  67. //生成小图 200px
  68. //$smallWidth = 200;
  69. //$smallHeight = ceil((200 * $height) / $width);
  70. //$smallFullPathFile = $path . $preFileName . '_small.' . $type;
  71. //Image::thumbnail($fullPathFile, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
  72. $data = business::formatUploadImg($img);
  73. // if (!empty($categoryIds)) {
  74. // $time = time();
  75. // $data = ['img' => $img, 'sjId' => $sjId, 'name' => '未命名', 'addTime' => $time];
  76. // $pic = PicService::add($data);
  77. // $currentId = $pic['id'];
  78. // $add = [];
  79. // foreach ($categoryIds as $cat) {
  80. // $add[] = ['categoryId' => $cat, 'picId' => $currentId, 'sjId' => $this->sjId];
  81. // }
  82. // PicCategoryService::batchAdd($add);
  83. // }
  84. $data['shortUrl'] = $img;
  85. util::success($data);
  86. }
  87. util::fail('上传失败!');
  88. }
  89. //保存图片 ssh 2019.12.20
  90. public function actionSave()
  91. {
  92. header('Content-type:text/html;charset=utf-8');
  93. $base64_image_content = '';
  94. //匹配出图片的格式
  95. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  96. $type = $result[2];
  97. $sjId = 12358;
  98. $month = date("Ym");
  99. $date = date("d");
  100. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  101. if (!file_exists($path)) {
  102. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  103. mkdir($path, 0700, true);
  104. }
  105. $preFileName = stringUtil::uniqueFileName();
  106. $newFile = $preFileName . ".{$type}";
  107. $fullPath = $path . $newFile;
  108. if (file_put_contents($fullPath, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  109. util::fail('上传失败');
  110. }
  111. $img = "/uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  112. $data = business::formatUploadImg($img);
  113. $data['shortUrl'] = $img;
  114. util::success($data);
  115. }
  116. }
  117. }