UploadController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace business\controllers;
  3. use common\components\business;
  4. use common\components\dirUtil;
  5. use common\components\stringUtil;
  6. use Yii;
  7. use common\components\util;
  8. use yii\imagine\Image;
  9. class UploadController extends BaseController
  10. {
  11. public $withoutLogin = [];
  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. $merchantId = 12358;
  22. $month = date("Ym");
  23. $date = date("d");
  24. $path = dirUtil::imgUploadDir() . "/{$merchantId}/{$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/{$merchantId}/{$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/{$merchantId}/{$month}/{$date}/" . $newFile;
  50. unlink($fullPathFile);
  51. $fullPathFile = $newFullPathFile;
  52. }
  53. //生成小图 200px
  54. $smallWidth = 200;
  55. $smallHeight = ceil((200 * $height) / $width);
  56. $smallFullPathFile = $path . $preFileName . '_small.' . $type;
  57. Image::thumbnail($fullPathFile, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
  58. $data = business::formatUploadImg($img);
  59. $data['shortUrl'] = $img;
  60. util::success($data);
  61. }
  62. util::fail('上传失败!');
  63. }
  64. //保存图片 shish 2019.12.20
  65. public function actionSave()
  66. {
  67. header('Content-type:text/html;charset=utf-8');
  68. $base64_image_content = '';
  69. //匹配出图片的格式
  70. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  71. $type = $result[2];
  72. $merchantId = 12358;
  73. $month = date("Ym");
  74. $date = date("d");
  75. $path = dirUtil::imgUploadDir() . "/{$merchantId}/{$month}/{$date}/";
  76. if (!file_exists($path)) {
  77. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  78. mkdir($path, 0700, true);
  79. }
  80. $preFileName = stringUtil::uniqueFileName();
  81. $newFile = $preFileName . ".{$type}";
  82. $fullPath = $path . $newFile;
  83. if (file_put_contents($fullPath, base64_decode(str_replace($result[1], '', $base64_image_content))) == false) {
  84. util::fail('上传失败');
  85. }
  86. $img = "/uploads/{$merchantId}/{$month}/{$date}/" . $newFile;
  87. $data = business::formatUploadImg($img);
  88. $data['shortUrl'] = $img;
  89. util::success($data);
  90. }
  91. }
  92. }