UploadController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace pt\controllers;
  3. use common\components\business;
  4. use common\components\dirUtil;
  5. use common\components\oss;
  6. use common\components\stringUtil;
  7. use common\components\util;
  8. use yii\imagine\Image;
  9. class UploadController extends BaseController
  10. {
  11. public $guestAccess = [];
  12. //上传图片接口
  13. public function actionSaveImg()
  14. {
  15. header('Content-type:text/html;charset=utf-8');
  16. $base64 = file_get_contents('php://input');
  17. $base64_image_content = str_replace("content=", "", urldecode($base64));
  18. //匹配出图片的格式
  19. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  20. $type = $result[2];
  21. $sjId = 0;
  22. $month = date("Ym");
  23. $date = date("d");
  24. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  25. if (!file_exists($path)) {
  26. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  27. mkdir($path, 0700, true);
  28. }
  29. $preFileName = stringUtil::uniqueFileName();
  30. $newFile = $preFileName . ".{$type}";
  31. $fullPathFile = $path . $newFile;
  32. if (file_put_contents($fullPathFile, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  33. util::fail('上传失败!!');
  34. }
  35. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  36. $imgInfo = getimagesize($fullPathFile);
  37. $width = $imgInfo[0];
  38. $height = $imgInfo[1];
  39. //如果图片太大,进行压缩,并删除原图
  40. if ($width > 800) {
  41. $newWidth = 800;
  42. $newHeight = ceil((800 * $height) / $width);
  43. $preFileName = stringUtil::uniqueFileName();
  44. $newFile = $preFileName . ".{$type}";
  45. $newFullPathFile = $path . $newFile;
  46. Image::thumbnail($fullPathFile, $newWidth, $newHeight)->save($newFullPathFile, ['quality' => 100]);
  47. $width = $newWidth;
  48. $height = $newHeight;
  49. $img = "uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  50. unlink($fullPathFile);
  51. $fullPathFile = $newFullPathFile;
  52. }
  53. //上传oss 2021.3.18 lqh
  54. oss::uploadImage($img,$fullPathFile);
  55. //生成小图 200px
  56. //$smallWidth = 200;
  57. //$smallHeight = ceil((200 * $height) / $width);
  58. //$smallFullPathFile = $path . $preFileName . '_small.' . $type;
  59. //Image::thumbnail($fullPathFile, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
  60. $data = business::formatUploadImg($img);
  61. $data['shortUrl'] = $img;
  62. util::success($data);
  63. }
  64. util::fail('上传失败!');
  65. }
  66. //保存图片 ssh 2019.12.20
  67. public function actionSave()
  68. {
  69. header('Content-type:text/html;charset=utf-8');
  70. $base64_image_content = '';
  71. //匹配出图片的格式
  72. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  73. $type = $result[2];
  74. $sjId = 12358;
  75. $month = date("Ym");
  76. $date = date("d");
  77. $path = dirUtil::getImgUploadDir() . "/{$sjId}/{$month}/{$date}/";
  78. if (!file_exists($path)) {
  79. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  80. mkdir($path, 0700, true);
  81. }
  82. $preFileName = stringUtil::uniqueFileName();
  83. $newFile = $preFileName . ".{$type}";
  84. $fullPath = $path . $newFile;
  85. if (file_put_contents($fullPath, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  86. util::fail('上传失败');
  87. }
  88. $img = "/uploads/{$sjId}/{$month}/{$date}/" . $newFile;
  89. $data = business::formatUploadImg($img);
  90. $data['shortUrl'] = $img;
  91. util::success($data);
  92. }
  93. }
  94. }