UploadController.php 8.7 KB

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