UploadController.php 7.0 KB

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