Upload.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\hander\HelpHander;
  4. use think\Controller;
  5. use think\Db;
  6. use think\Exception;
  7. class Upload extends Controller
  8. {
  9. public function upfile(){
  10. $file = request()->file('uploadFile');
  11. // 移动到框架应用根目录/uploads/ 目录下
  12. $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
  13. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
  14. if($info){
  15. $img = '/uploads/files/' . $info->getSaveName();
  16. $img = str_replace('\\', '/', $img);
  17. HelpHander::success(['path' => config("app.app_host").$img]);
  18. }else{
  19. // 上传失败获取错误信息
  20. HelpHander::error($file->getError());
  21. }
  22. }
  23. public function upimg(){ // 只能上传规定的图片,添加水印,时间 地点 人物 模块
  24. $file = request()->file('uploadFile');
  25. // 移动到框架应用根目录/uploads/ 目录下
  26. $config = config('max_upload_img');
  27. $info = $file->validate($config)
  28. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
  29. if($info){
  30. $img = '/uploads/files/' . $info->getSaveName();
  31. $img = str_replace('\\', '/', $img);
  32. try{
  33. $this->thumb($img);
  34. $image = \think\Image::open('.'.$img);
  35. $width = $image->width();
  36. $height = $image->height();
  37. $size = 100;
  38. $curSize = round($size*$width/2000);
  39. $h = 100;
  40. $curH = round($h*$width/2000);
  41. $address = input('address', '', 'trim');
  42. $mode = input('mode', '', 'trim');
  43. $userId = input('userId/d', 0);
  44. $uname = Db::name('user')->where('id', $userId)->value('real_name');
  45. $font = env('root_path') . 'public/static/hylht.ttf';
  46. $i = 3;
  47. if($address){
  48. $i = 4;
  49. }
  50. $arr1 = $this->getSize(date('Y-m-d H:i:s'),$curSize,$width,$height,$i,$curH);
  51. $arr2 = $this->getSize($mode,$curSize,$width,$height,$i-1,$curH);
  52. if($address){
  53. $arr3 = $this->getSize($address,$curSize,$width,$height,2,$curH);
  54. }
  55. $arr4 = $this->getSize($uname,$curSize,$width,$height,1,$curH);
  56. $image->text(date('Y-m-d H:i:s'), $font, $curSize, '#ffffff', $arr1);
  57. $image->text($mode, $font, $curSize, '#ffffff', $arr2);
  58. if($address){
  59. $image->text($address, $font, $curSize, '#ffffff', $arr3);
  60. }
  61. $image->text($uname, $font, $curSize, '#ffffff', $arr4)->save('.' . $img);
  62. }catch (\Exception $e){
  63. trace('图片上传失败:'.$e->getMessage());
  64. HelpHander::error('图片上传失败');
  65. }
  66. HelpHander::success(['path' => config("app.app_host").$img]);
  67. }else{
  68. // 上传失败获取错误信息
  69. HelpHander::error($file->getError());
  70. }
  71. }
  72. private function thumb($img){
  73. $image = \think\Image::open('.'.$img);
  74. $width = $image->width();
  75. $height = $image->height();
  76. if($width > 2000||$height > 2000){
  77. $image->thumb(2000,2000,\think\Image::THUMB_SCALING)->save('.' . $img);
  78. }
  79. }
  80. private function getSize($text,$size,$weight,$height,$i=1,$ch=100,$angle=0){
  81. //获取文字信息
  82. $font = env('root_path').'public/static/hylht.ttf';
  83. $info = imagettfbbox($size, $angle, $font, $text);
  84. $minx = min($info[0], $info[2], $info[4], $info[6]);
  85. $maxx = max($info[0], $info[2], $info[4], $info[6]);
  86. $miny = min($info[1], $info[3], $info[5], $info[7]);
  87. $maxy = max($info[1], $info[3], $info[5], $info[7]);
  88. /* 计算文字初始坐标和尺寸 */
  89. $x = $minx;
  90. $y = abs($miny);
  91. $w = $maxx - $minx;
  92. $h = $maxy - $miny;
  93. $xx = ($weight - $w) / 2;
  94. $yy = $height - ($h+$ch)*$i;
  95. return [$xx,$yy];
  96. }
  97. public function uply(){
  98. trace($_FILES);
  99. $file = request()->file('filedata1');
  100. // 移动到框架应用根目录/uploads/ 目录下
  101. $info = $file->validate([ 'ext' => 'wav' ])
  102. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'luyin');
  103. if($info){
  104. $img = '/uploads/luyin/' . $info->getSaveName();
  105. $img = str_replace('\\', '/', $img);
  106. $img = config("app.app_host").$img;
  107. trace($img);
  108. HelpHander::success(['path' => $img]);
  109. }else{
  110. // 上传失败获取错误信息
  111. HelpHander::error($file->getError());
  112. }
  113. }
  114. }