StorageController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * 文件上传模块
  4. * @author wesmiler
  5. */
  6. namespace app\api\controller;
  7. use app\admin\model\CarModel;
  8. use app\weixin\model\Commands;
  9. use app\weixin\model\FileLogs;
  10. use app\weixin\model\Member;
  11. use app\weixin\model\Storage;
  12. use app\weixin\model\Wechat;
  13. use app\weixin\service\PRedis;
  14. use cmf\controller\HomeBaseController;
  15. class StorageController extends HomeBaseController
  16. {
  17. /**
  18. * 图片上传
  19. */
  20. public function upload(){
  21. $file = request()->file('image');
  22. $scene = request()->get('scene','image');
  23. $water = request()->get('water',0);
  24. $fileData = Storage::uploadImg($file, $scene, $water);
  25. showJson(1005, 3002, $fileData);
  26. }
  27. /**
  28. * 图片上传
  29. */
  30. public function thumb(){
  31. $file = request()->file('image');
  32. $scene = request()->get('scene','image');
  33. $fileData = Storage::uploadImg($file, $scene);
  34. showJson(1005, 3002, $fileData);
  35. }
  36. /**
  37. * 认证图片上传
  38. */
  39. public function auth(){
  40. $file = request()->file('image');
  41. $scene = request()->get('scene','auth');
  42. $water = request()->get('water',1);
  43. $fileData = Storage::uploadImg($file, $scene, false);
  44. $siteInfo = cmf_get_site_info();
  45. $waterTxt = isset($siteInfo['water_txt']) ? trim($siteInfo['water_txt']) : '此证件仅限于拾光单身平台使用';
  46. if($waterTxt && $water){
  47. $img = isset($fileData['file']) ? $fileData['file'] : '';
  48. $img = Storage::imageWater($img, $waterTxt);
  49. $fileData['file'] = $img;
  50. $fileData['preview'] = cmf_get_image_preview_url($img);
  51. }
  52. showJson(1005, 3002, $fileData);
  53. }
  54. /**
  55. * 图片压缩
  56. * @return string
  57. */
  58. public function imgThumb(){
  59. $file = input('url');
  60. //PRedis::set('test:file1', $file, 600);
  61. if($file){
  62. header("Content-Type:image/jpeg");
  63. $type = input('t', 1);
  64. $params = [
  65. 'width'=> input('w', 1500),
  66. 'height'=> input('h', 1500),
  67. ];
  68. $strs = explode("/upload/", $file);
  69. $filename = isset($strs[1])? $strs[1] : '';
  70. //PRedis::set('test:file', $filename, 600);
  71. if($filename){
  72. $baseName = basename($filename);
  73. $cacheKey = 'files:thumb:'.substr($baseName,0,6).'_'.md5($filename).'_'.$params['width'].'_'.$params['height'].'_'.$type;
  74. $fileContent = PRedis::get($cacheKey);
  75. if($fileContent){
  76. echo $fileContent;
  77. exit;
  78. }
  79. if(file_exists('upload/'.$filename)){
  80. $file = Storage::imageThumb($filename, $type, $params);
  81. if(file_exists('upload/'.$file)){
  82. $fileContent = file_get_contents('upload/'.$file);
  83. PRedis::set($cacheKey, $fileContent, 6*3600);
  84. echo $fileContent;
  85. exit;
  86. }
  87. }
  88. }else{
  89. $cacheKey = 'files:thumb:'.md5($file);
  90. $fileContent = PRedis::get($cacheKey);
  91. if($fileContent){
  92. echo $fileContent;
  93. exit;
  94. }
  95. $fileContent = file_get_contents($file);
  96. PRedis::set($cacheKey, $fileContent, 6*3600);
  97. echo $fileContent;
  98. exit;
  99. }
  100. }
  101. header("Content-Type:image/png");
  102. echo file_get_contents('themes/default/weixin/public/assets/img/no-pic.png');
  103. exit;
  104. }
  105. }
  106. ?>