UploadController.php 7.0 KB

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